Setattribute Delayed?

Hello! I was making a Stun system using StunHandler, but the stun has a delay before activating and it creates a weird experience for players who start attacking at the same time. How can I fix the delay?

The Stun uses an attribute.

Stunhandler Code (i made it native)
--!native
-- Credits to ApprenticeOfMadara

--[[
	Very simple to use stun module:
	Simply send a humanoid object and the amount of seconds you want the stun to last for to the function
	
	Example:
		local StunHandler = require(path.to.module)
		
		StunHandler.Stun(humanoid, 5)	-->>	stuns for 5 seconds
		
	The function saves the current speed and jump power of the humanoid before setting them to 0
	Once the specified duration passes, it will reset the speed and jump power back to what was stored
	
	If the function tries to stun an already stunned humanoid, it will compare the duration sent with the time left
	from the previous stun
	If there is less time left, the stun duration will update to the new one
	
	An attribute with the name "Stunned" is added to the humanoid's parent (character) and set to true whilst
	the humanoid is stunned
	It is set to false again once the stun wears off
	
	Note: The function does not yield whilst the humanoid has been stunned
]]

--//Private variables & functions
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
		-- 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
	
	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
		
		humanoid.WalkSpeed = 6
		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 = 6
			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}
1 Like

Put a print() before and after the line, to see if any delay does happen. If it doesn’t, then it’s something else in the code that’s causing the issue.

There’s a visible delay in properties when I look at the attribute when it’s being set.

Then print what the current attribute value is after the line. The actual properties window can have a delay in studio.

1 Like

Apparently it has no delay, but some players can still attack after being hit. When a player attacks, I have this check:

if AttackDebounce[plr] or plr:GetAttribute("Stunned") or plr:GetAttribute("Ragdolled") or (enemy.HumanoidRootPart.Position - plr.Character.HumanoidRootPart.Position).Magnitude > 12 then return end

but apparently that isn’t working
do you know what can be causing it?

I would make a separate if statement for everything on that line, alongside a print(), to see which condition isn’t met.

Also a 12-stud distance is a bit too far away as a minimum, you should set it to a 4 or 6.

1 Like

I have to go, i’ll test it tomorrow and tell you