Help with stun system

I’ve been wondering for days on how I could make a good stun system but fail to think of a good way to make one. The one i’m currently using now involves a Runservice a an IF function that detects if a instance with the name block is inside the character and then proceeds to set the humanoid walkspeed to 2 and the jumppower to 0 and if there isn’t instance with the name block then the humanoid walkspeed and jumpower is 12 and 45; heres an example of the script:

local PlayerService = game:GetService("Players")
local Player = PlayerService.LocalPlayer
local Character = Player.Character
local Humanoid = Character.Humanoid
local HumanoidRootPart = Character.HumanoidRootPart

game:GetService("RunService").RenderStepped:Connect(function()

if Character:FindFirstChild("Stunned") then
Humanoid.Walkspeed = 2
Humanoid.JumpPower = 0
else
Humanoid.Walkspeed = 12
Humanoid.JumpPower = 45
end)

Now you may be wondering what’s wrong with this ? Nothing but there are bound to be bugs happening from time to time with this type of stun system. The most you could do is create a IF function for something that shouldn’t trigger the else like;

local PlayerService = game:GetService("Players")
local Player = PlayerService.LocalPlayer
local Character = Player.Character
local Humanoid = Character.Humanoid
local HumanoidRootPart = Character.HumanoidRootPart

game:GetService("RunService").RenderStepped:Connect(function()

if Character:FindFirstChild("Stunned") then
Humanoid.Walkspeed = 2
Humanoid.JumpPower = 0
else
if Character:FindFirstChild("Sprint") then
	Character.Humanoid.WalkSpeed = 21
	Character.Humanoid.JumpPower = 42.5
	return
elseif Character:FindFirstChild("Block") then
	Character.Humanoid.WalkSpeed = 8
	Character.Humanoid.JumpPower = 42.5
	return
elseif Character:FindFirstChild("Dash") then
	return
end
Humanoid.Walkspeed = 12
Humanoid.JumpPower = 45
end
end)

But as you can assume their will be times this doesn’t function properly depending if you time it right ( It’s hard but still possiable).

Could anyone please suggest me a new viable method!

First thing I could think of is the delay caused from the time it takes to create an instance (though likely not too noticeable), however, you should check out Attributes which is a much better approach to this.
After that you could use Instance.AttributeChanged to detect stuns and form some kind of connections between the states. Ex: Stunned - No block - No sprint - No dash, Block - No sprint - No dash,…
Example script:

Humanoid.AttributeChanged:Connect(function(AttributeChanged)
    if AttributeChanged == 'Stunned' then
       value = Humanoid:GetAttribute('AttributeChanged')
       if value == true then
           --stun player
       elseif value == false then
           --stop stuns
       end
    end
    if AttributeChanged == 'Sprint' and Humanoid:GetAttribute('Stunned') == false and Humanoid:GetAttribute('Block') == false then
       --check true false value same as above
    end
end)

This is how I did this some time ago. It worked quite well with no bugs I believe, though there wasn’t enough time devoted to testing so there’s no guarantee but you can try it out.

Copied the code into a local script and looks like there’s an error at the end of the function parenthesis, please be more precise with your code
Screenshot 2022-07-22 230909