Issue with my stun system

Okay so I have a stun system where basically everytime u get hit it adds X amount of stun
every amount of stun is a second that ur stun lasts
e.g u have 3 stun your stun lasts for 3 seconds

Here is my current code:

stundur:GetPropertyChangedSignal("Value"):Connect(function()
	local hum = char:WaitForChild("Humanoid")
	if stundur.Value >= 1 then
		if stundur.Value >= 8 then
			stundur.Value = 1
		end
		if not stunned.Value then
			stunned.Value = true
			char.Humanoid.WalkSpeed = 1
			hum.JumpPower = 0
			wait(1)
			stundur.Value = stundur.Value - 1
		else
			wait(1)
			stundur.Value = stundur.Value - 1
		end
	else
		stundur.Value = 0
		if ragdoll.Value then
			repeat
				wait(0.1)
			until ragdoll.Value == false
		end
		if timestop.Value then
			repeat
				wait(0.1)
			until timestop.Value == false
		end
		if blocking.Value == true then
			repeat
				wait(0.1)
			until blocking.Value == false
		end
		stunned.Value = false
		char.Humanoid.WalkSpeed = 16
		hum.JumpPower = 60
	end
end)

Pretty much the issue here is that everytime stun changes it waits 1 then removes stun.
with my combat system if I hit someone 3 times and it stuns for 1 for every hit
everytime it takes one stun the value changes therefore it waits a second and removes one but when I add stun it also triggers this resulting in stun being removed faster than it should.
What is a good solution to this problem? Its really hard to explain so I hope I did a reasonable job at it.

I thought of making a while loop that every second takes stun if it exists however lets say I hit someone and the loop has already waited .9 out of the second it waits before it removes stun. the stun would essentially not register and only be .1 seconds.

All help is appreciated I know its really confusing lol

1 Like