How to keep Stunning after getting Hit?

but it should work fine in the server script service or it doesnt really matter?

Yes, it could work anywhere. Preferably it would be put in a server storage object like ServerScriptService.

1 Like

Alright, I’ll keep this in mind whenever I’m making games. Thank you!

You can try a “global task” feature by making a playerStuns global table and filtering by the player’s name, including the task.spawn() and task.cancel() function, avoiding that the wait() “combos”:


local StunModule = {}
local playerStuns = {}
function StunModule:Stun(Character:Instance, Duration: number, StunWalkSpeed: number, StunJumpPower: number)
	local Humanoid = Character:WaitForChild("Humanoid")

	if Humanoid then
		if Humanoid.Health <= 0 then
			return
		end
	else
		return
	end
	if not playerStuns[Character.Name] then
		playerStuns[Character.Name] = {
			WS = Humanoid.WalkSpeed,
			JP = Humanoid.JumpPower
		}
	end
	local WalkSpeed = playerStuns[Character.Name].WS
	local JumpPower = playerStuns[Character.Name].JP
	if playerStuns[Character.Name].Task then task.cancel(playerStuns[Character.Name].Task) end
	playerStuns[Character.Name].Task = task.spawn(function()

		Humanoid:SetAttribute("Stunned", true)
		Humanoid.WalkSpeed = StunWalkSpeed
		Humanoid.JumpPower = StunJumpPower

		wait(Duration) --// This is the issue.

		Humanoid:SetAttribute("Stunned", false)
		Humanoid.WalkSpeed = WalkSpeed
		Humanoid.JumpPower = JumpPower
		warn("Ended")
		playerStuns[Character.Name] = nil
	end)
end

return StunModule