-
What do you want to achieve? Keep it simple and clear!
I want to make a stun system, Basically, there’s an intValue placed in every player’s char, every time it changes its supposed to substract 1 every second and stop when its at 0 again
-
What is the issue? Include screenshots / videos if possible!
It does it way too fast, you dont even see the difference, the stun doesnt even last a milisecond!
-
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I tried adding a wait() but when i tested it, it doesnt stop, i also tried making a while loop and breaking it when it reaches 0 but it had the same result
This is the script im using:
game.Players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function(char)
local stun = char.stunned
stun:GetPropertyChangedSignal("Value"):Connect(function()
print("new value is" .. stun.Value)
repeat
stun.Value -= 1
until stun.Value == 0
end)
end)
end) -- yes its pretty basic, i know.
what do i do to fix this? thanks!
if you add a wait() it waits 1 frame, add a wait(1), the number in the brackets is the seconds
It gives me the same result it doesnt seem to stop when the value is at 0, why is this happening?
The reason is that the repeat until runs once before checking if its 0, you’ll need to use a different loop for this.
Could i use a for i,v in pairs for this?
No, a in pairs loop is used for looping through tables, you could use a for loop though, like this:
local stunval = stun.Value
for i = 1, stunval do
stun.Value -=1
wait(1)
end
It keeps giving me the same result somehow.
I see, that’s cause you fire it again cause the valueis changed and the for loop runs over and over, multiple times at once
you can fix this by doing something like adding a debounce:
game.Players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function(char)
local stun = char.stunned
local debounce = false
stun:GetPropertyChangedSignal("Value"):Connect(function()
local stunval = stun.Value
if debounce == false then
debounce = true
for i = 1, stunval do
stun.Value -=1
wait(1)
end
debounce = false
end
end)
end)
end) -- yes its pretty basic, i know.
This does work, Thanks alot!! (i added some changes but pretty much the same thing)