• How to make a drowning system
    6 replies, posted
I want to simulate drowning but am unsure how to do it. I created a small function below that I thought would do it but it doesnt seem to work, or maybe I'm not putting it in the correct spot. I want a player to start drowning after 15 seconds if they are fully submerged. I then want it to take 15 hp every second once they start to drown. I feel like the way I did it below is a very inefficient way of doing it and it seems like gmod should already have some type of function like this but I couldnt find one. Any help would be greatly appreciated. Mainly what I need help on: - Where to put the drowning code - Is my code correct, if not what should I do to fix it. - Anywhere else I need code such as ShouldEntityTakeDamage [lua] if !ValidEntity(ply) then return end if ply:WaterLevel() != 3 then return end timer.simple(15, function() timer.Create( "drown_timer", 1, 0, function() ply:TakeDamage(15,GetWorldEntity(),GetWorldEntity()) end) end) [/lua]
[QUOTE=thejjokerr;26587220]This might work: [lua]hook.Add("Think", "CheckIfDrowning", function() for k, v in pairs( player.GetAll() )do if (!v.nextCheckDrowning or v.nextCheckDrowning < CurTime() ) then if (v:WaterLevel() == 3) then v:TakeDamage(15,GetWorldEntity(),GetWorldEntity()) end; v.nextCheckDrowning = CurTime() + 15; end; end; end);[/lua][/QUOTE] This code is a bad idea.
[QUOTE=thejjokerr;26587695]Oh, whats wrong with it?[/QUOTE] [b][url=wiki.garrysmod.com/?title=Gamemode.HandlePlayerSwimming]Gamemode.HandlePlayerSwimming [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b] You should probably stick it there.
[lua] hook.Add("Think","lol",function() for k,v in pairs(player.GetAll()) do if v:WaterLevel() < 3 then v.DrownTimer = nil continue end v.DrownTimer = v.DrownTimer or CurTime()+15 if v.DrownTimer < CurTime() then v:TakeDamage(15,v,v) v.DrownTimer = CurTime()+1 --faster drowning once you run out of oxygen end end end) [/lua]
EDITED: I got it kinda working but it seems to only work with only 1 player. Any ideas why? [LUA] function GM:HandlePlayerSwimming(ply) if !ValidEntity(ply) then return end if ply.timerrunning == nil then ply.timerrunning = false end if !timer.IsTimer("drown_delay") then timer.Create("drown_delay", 10, 1, test1, ply)--(ply)) timer.Stop("drown_delay") end if !timer.IsTimer("drown_timer") then timer.Create("drown_timer", 1, 0, test2, ply)--(ply)) timer.Stop("drown_timer") end if ply:WaterLevel() == 3 && !ply.timerrunning then ply.timerrunning = true elseif ply:WaterLevel() != 3 && ply.timerrunning then timer.Stop("drown_delay") timer.Stop("drown_timer") ply.timerrunning = false end end function test1(ply) timer.Start("drown_timer") end function test2(ply) if ply:WaterLevel() == 3 then ply:TakeDamage(20,GetWorldEntity(),GetWorldEntity()) end end [/LUA] [editline]9th December 2010[/editline] Got it working! Now how do I set the damagetype to be drowning so it plays the drowning sound and give the drowning effect? I saw this: [url]http://wiki.garrysmod.com/?title=CTakeDamageInfo.SetDamageType[/url] but it didnt help.
Sorry, you need to Log In to post a reply to this thread.