Module Origin:Stun Handler V2.1 - #18 by crazygamespp
I ran into an issue while using this module.Everything works fine until I implemented a few lines for the stun to work more well with my game.The problem basicly makes all players who ever been stunned stay stunned forever.
Can anyone maybe help with what’s the problem with my module.
Help is appreciated!
“CanAttack” is just a Bool Value thats located in a player character to determine if he or she is allowed to use a move.
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
--local CanAttack = humanoid.Parent.CanAttack
humanoid.Parent.CanAttack.Value = data.stunbool
humanoid.Parent:SetAttribute("Stunned", false)
end
end
stunnedHumanoids += 1
-- print(string.format("waiting: %f", data.duration - currentTime))
end
if stunnedHumanoids == 0 then
checkConnection:Disconnect()
isChecking = false
end
end
--//Stun Handler
return {Stun = function (humanoid, duration, CanAttack)
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 = humanoid.WalkSpeed
data.jumpPower = humanoid.JumpPower
data.jumpHeight = humanoid.JumpHeight
data.stunbool = humanoid.Parent.CanAttack.Value
humanoid.WalkSpeed = 0
humanoid.JumpPower = 0
humanoid.JumpHeight = 0
CanAttack.Value = false
humanoid.Parent:SetAttribute("Stunned", true)
elseif data.duration - currentTime < duration then -- update duration if less time left
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
CanAttack.Value = false
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}
--local StunHandler = require(game.ReplicatedStorage.StunHandlerV2)
--StunHandler.Stun(hit.Parent.Humanoid, 10, hit.Parent.CanAttack)