So I’m trying to make a hitstun system for my game. Heres what I have:
function Stun(Player,Time)
local hs = Player:WaitForChild('Hitstun')
hs.Value = true
wait(Time)
hs.Value = false
end
The problem with this is if the player gets hit once its absolutely fine. But if they get hit again the function runs again, meaning that the original function will set the hitstun value to false before the second one is down playing. A possible solution for that would be to reset the wait each new time a player gets hit. I have no clue how to do this though.
Instead of using a boolean value you could use an int or number value that tracks the stun time for each player. That way you can determine whether multiple stuns should either stack up or reset the timer.
function Stun(Player, Time)
local stunned = Player:FindFirstChild("StunTime")
if (not stunned) then
stunned = Instance.new("IntValue")
stunned.Name = "StunTime"
stunned.Value = Time
stunned.Parent = Player
spawn(function()
while (stunned.Value > 0) do
-- code that keeps the player stunned goes here
stunned.Value = stunned.Value - 1
wait(1)
end
stunned:Destroy()
-- code to un-stun the player goes here
end)
else
-- Use this line if you want additional stuns to RESET the timer.
stunned.Value = Time
-- Use this line if you want additional stuns to ADD to the timer.
--stunned.Value = stunned.Value + Time
end
end
local stunTimes = {}
local function stun(player, time)
local hs = player:WaitForChild("HitStun")
hs.Value = true
stunTimes[player] = tick()+time
wait(time)
if stunTimes[player] <= tick() then
hs.Value = false
stunTimes[player] = nil
end
end