Stun Module Error?

I used this stun module from: Stun Handler V2.1 - #18 by crazygamespp
It sometimes for some reason stuns but doesn’t un-stun, randomly sometimes. Idk why and don’t really know how to fix it.

Code:

local players = game:GetService("Players")
local heartbeat = game:GetService("RunService").Heartbeat

local Stunned = {}

local clock = os.clock
local isChecking = false
local checkConnection
local currentTime
local stunnedHumanoids

local function stunChecker()
	print("Stun Checker?")
	currentTime = clock()
	stunnedHumanoids = 0

	for humanoid, data in pairs(Stunned) do
		print(data, data.stunned)
		if not data.stunned then continue end
		
		print("stunned")

		if currentTime >= data.duration then
			data.stunned = false

			data.changedConn:Disconnect()
			data.changedConn = nil

			if humanoid:IsDescendantOf(workspace) then
				print("workspace")
				humanoid.WalkSpeed = data.speed
				humanoid.JumpPower = data.jumpPower
				humanoid.JumpHeight = data.jumpHeight

				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)
	if humanoid.Health <= 0 then return end

	local Character = humanoid.Parent
	if not Character 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

		if humanoid.JumpPower <= 0 then
			data.jumpPower = 50
		else
			data.jumpPower = humanoid.JumpPower
		end

		if humanoid.JumpHeight <= 0 then
			data.jumpHeight = 7.2
		else
			data.jumpHeight = humanoid.JumpHeight
		end

		humanoid.WalkSpeed = 0
		humanoid.JumpPower = 0
		humanoid.JumpHeight = 0

		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
		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}

If you’re using Heartbeat, try creating vars to store the start and end times of the stun duration; I say this mainly because os.clock() isn’t always as precise when it comes to multiple users, because its based upon CPU time, meaning the stun time will differ between the server and the client.

1 Like