Countdown Time Thing?

Hi everyone, On my server im trying to have it so if you get shot by a certian listed Weapon it will make you slow down for a Period of time, like maybe 3-5 seconds. But with the line of Code I have, once you get shot you stay at the Slow speed… I was wondering how would I make it so that once you get shot, it sets you at the slow speed, then counts down to my selected time, then resets you back to the normal walk speed.

[lua] function PlayerShouldTakeDamage(ply, attacker)
if (attacker:IsNPC() or attacker:IsPlayer()) then
if (attacker:GetActiveWeapon():GetClass() == “weapon_pistol”) then
ply:SetWalkSpeed(45)
end
end
end
hook.Add(“PlayerShouldTakeDamage”, “PlayerShouldTakeDamage”, PlayerShouldTakeDamage)[/lua]

Try this:

[lua]
timer.Simple(5, function() ply:SetWalkSpeed(250) end) – Returns to default walkspeed (250) after 5 seconds.
[/lua]

Why not **[Gamemode.EntityTakeDamage

http://wiki.garrysmod.com/favicon.ico](wiki.garrysmod.com/?title=Gamemode.EntityTakeDamage)** ?
It has “built-in” inflictor (weapon class) var in it.

Aurum is also right, you should use **[Gamemode.EntityTakeDamage

http://wiki.garrysmod.com/favicon.ico](wiki.garrysmod.com/?title=Gamemode.EntityTakeDamage)** unless you want to stop the player from getting injured by returning false.

Im really Sorry, I dont know Shit about Coding, I can edit, add small parts, But I dont understand Gamemode.EntityTakeDamage, With it its possible that if someone gets shot, they slow down for like 3 seconds?

Well using the above function you are being given the weapon, attacker, amount of damage, and the info of the damage, as well as the victim ofc. That way you can shorten your code to if( Inflictor == “weapon_pistol” ) then and that’s all I know of how it will help your code.

Bu all you have to do to slow them is like…
[lua]
ply:SetWalkSpeed(50)
timer.Simple(5, function() ply:SetWalkSpeed(250) end)
[/lua]

in line 4. :slight_smile:

Here’s some better code, tested and seems to work well. This needs to go somewhere serverside:

[lua]
if SERVER then – To ensure it doesn’t run clientside.
hook.Add(“EntityTakeDamage”, “SlowDownPlayer”, function(ply, inflictor, attacker)
if ply:IsValid() && ply:IsPlayer() && attacker:IsValid() && (attacker:IsNPC() || attacker:IsPlayer()) && inflictor:IsValid() then
if inflictor:GetClass() == “weapon_pistol” || attacker:GetActiveWeapon():GetClass() == “weapon_pistol” then
ply:SetWalkSpeed(45)
ply:SetRunSpeed(90) – So they can’t run away instead
if timer.IsTimer(“Player_SlowMove_Timeout”) then
timer.Start(“Player_SlowMove_Timeout”)
else
timer.Create(“Player_SlowMove_Timeout”, 3, 1, function() ply:SetWalkSpeed(250) ply:SetRunSpeed(500) end)
end
end
end
end)
end
[/lua]

Thank you Freeman!

But how would I add multiple weapons?

then do this and edit the WeaponsTable table

[lua]
local WeaponsTable = { “weapon_pistol”, “weapon_smg”, “weapon_ar2” }

if SERVER then – To ensure it doesn’t run clientside.
hook.Add(“EntityTakeDamage”, “SlowDownPlayer”, function(ply, inflictor, attacker)
if ply:IsValid() && ply:IsPlayer() && attacker:IsValid() && (attacker:IsNPC() || attacker:IsPlayer()) && inflictor:IsValid() then
if table.HasValue( WeaponsTable, inflictor:GetClass() ) || table.HasValue( WeaponsTable, attacker:GetActiveWeapon():GetClass() ) then
ply:SetWalkSpeed(45)
ply:SetRunSpeed(90) – So they can’t run away instead
if timer.IsTimer(“Player_SlowMove_Timeout”) then
timer.Start(“Player_SlowMove_Timeout”)
else
timer.Create(“Player_SlowMove_Timeout”, 3, 1, function() ply:SetWalkSpeed(250) ply:SetRunSpeed(500) end)
end
end
end
end)
end
[/lua]

Thank you! I think im learning more lua everyday :3

I feel ya, I learned it myself in threads very similar to these :slight_smile:

Hmm, it only works for the Pistol still.

Dosent have to be like something weapons table instead of pistol?[lua]etClass() == “weapon_pistol”[/lua]

Whoops. My mistake. I fixed that I think, give it another test :slight_smile:

I am still learning Lua the way you are, bullard AND by helping people like you. :slight_smile:

Thank you! See, 4 days ago, I wouldn’t of understood why it wasn’t working! Your a Life saver.