I found a stun system, but there is a problem in my game, when I hit 1 time, the user is stunned, but when hitting consecutive hits he simply does not get stun and can walk normally.
local heartbeat = game:GetService("RunService").Heartbeat
local Stunned = {}
local clock = os.clock
local isChecking = false
local checkConnection
local currentTime
local stunnedHumanoids
local function stunChecker()
currentTime = clock()
stunnedHumanoids = 0
for humanoid, data in pairs(Stunned) do
if not data.stunned then
continue
end
if currentTime >= data.duration then
data.stunned = false
data.changedConn:Disconnect()
data.changedConn = nil
if humanoid:IsDescendantOf(workspace) then
humanoid.WalkSpeed = data.speed
humanoid.JumpPower = data.jumpPower
humanoid.JumpHeight = data.jumpHeight
humanoid.Parent:SetAttribute("Stunned", false)
end
end
stunnedHumanoids += 1
end
if stunnedHumanoids == 0 then
checkConnection:Disconnect()
isChecking = false
end
end
--//Stun Handler
return {
Stun = function(humanoid, duration)
if humanoid.Health <= 0 then
return
end
if not Stunned[humanoid] then
Stunned[humanoid] = {}
end
local data = Stunned[humanoid]
currentTime = clock()
if not data.stunned then -- not stunned
data.stunned = true
data.duration = currentTime + duration
data.speed = 10
data.jumpPower = humanoid.JumpPower
data.jumpHeight = 7.2
humanoid.WalkSpeed = 0
humanoid.JumpPower = 0
humanoid.JumpHeight = 0
humanoid.Parent:SetAttribute("Stunned", true)
print(data.speed, data.jumpPower, data.jumpHeight, data.duration)
elseif data.duration - currentTime <= duration then
data.duration = currentTime + duration
end
if not data.changedConn then
data.changedConn = humanoid.Changed:Connect(function()
humanoid.WalkSpeed = 0
humanoid.JumpPower = 0
humanoid.JumpHeight = 0
end)
end
if not data.diedConn then
data.diedConn = humanoid.AncestryChanged:Connect(function()
data.diedConn:Disconnect()
if data.changedConn then
data.changedConn:Disconnect()
end
Stunned[humanoid] = nil
end)
end
if not isChecking then
isChecking = true
checkConnection = heartbeat:Connect(stunChecker)
end
end,
}
The problem is that in my local combat script, first I give it a speed of 2 and then 10 but at no time do I see the walkspeed change to 0
But I commented on that and now the problem I have is that it does not add the time, when the user is already stunned