Countdown script is not working?

So I have this script that is supposed to decrease 1 in the value its a child of, but it does not work? I clone this script and parent it to the Leaderstat value.

while true do
	if script.Parent.Name == "JailTime" then
		repeat
		script.Parent.Value = script.Parent.Value - 1
		wait(1)
		until script.Parent.Value == 0
	end
end
1 Like
while wait() do
   if script.Parent.Name == "JailTime" then
	  for i = script.Parent.Value, 0, -1 do
         script.Parent.Value = i
		 wait(1)
	  end
	-- Do stuff
   end
end

Edits: Fixed some errors

3 Likes

@LeoBlackbane’s method should work here but I’d suggest altering the code to work with the Changed event and only run when it has to. Otherwise this is going to lead you into a lot of performance issues as you get more players / more values

2 Likes

Don’t use while wait() do
There is no benefits, it just slows down your code.
The While-Wait-Do Idiom, by cntkillme: Addressing while wait() loops

local Time = script.Parent.Value
if script.Parent.Name == "JailTime" then
while Time > 0 do
Time = Time -1
wait(1)
end
--function you have once time = 0
end
1 Like

you should use

script.Parent.Name.Changed:Connect(function()
	if script.Parent.Name == "JailTime" then
		for i = 1, numberoftimesyouwanttorepeat do
           wait(1)
        end
	end
end)

for i loops are better because they don’t accidentally do too much, as while loops sometimes do.

1 Like

This would work however it is impractical and prone to breaking under extreme sever lag.

Oh this post. It’s not necessarily that it slows down your code, since inputting wait in either the condition or the iteration itself has the same behaviour. The problem is that wait is blatant abuse of the while condition due to the fact that it returns a truthy value and it strips you of your freedom to control iteration yielding. It’s bad code.

In some cases where developers have a non-terminating loop, they include a useless conditional and break check in their iteration, whereas you can just include that in the condition so iterations stop when the condition fails to pass. You may also want to control iteration waiting (e.g. a retry function will wait 3 seconds if the function fails but doesn’t wait any time if it passes, thus voiding any waits and stopping iteration instantaneously).

wouldn’t a while loop be worse? it runs even though it’s not necessary, slowing the gameplay down

No because with certain while loops like the one that I have it will only run when Time > 0.

no, it runs when the name is changed, and runs a specific amount of time

That’s why you use the conditional.

while jailTime > 0 do
    jailTime = jailTime - 1
end

Runs for the duration of jailTime being greater than 0. The trick to while loops is to terminate them when you do not need them anymore and only spawn them up when they are necessary. As for “slowing gameplay down”, while loops do not do this unless you are running expensive code in each iteration. It does not inherently truck performance alone.

DevConX’s while loop is the proper way to tackle this situation.


@DevConX The script TOP_Crundee123 posted is only impractical, it does not break under high server latency. Control over iterations is stripped due to an anchored numerical iteration across 1 to the jailTime number in ascending order.

3 Likes

Question – Why are you checking if the parent’s name is ‘JailTime’?

Because in the main original post that is what he checks for

1 Like

I tried this code, by changing the jailtime name to script.Parent.Value and it does not work?

You aren’t supposed to use the code I provided because I was using that as a format to explain something to someone else. Please refer to the examples others have posted on this thread or the concepts they’ve set forth to modify your code.

This is what I whipped together. So I have a few questions.

local Time = script.Parent.Value

if script.Parent.Name == "JailTime" then

while Time > 0 do

Time = Time -1

wait(1)

end

if Time == 0 then

local player = script.Parent.Parent.Parent

player.stats.Arrested = false

player:LoadCharacter()

player.Team = nil

end

end

So firstly, if we already got the “Time” from the parent, and then check if the parents name is JailTime, that doesn’t make much sense. Next, this does not work. It was not removing any value from the value. Lastly, I have no idea how to change a players team to a nothing.

1 Like

Also, why wouldn’t it be counting down?

1 Like

Its not removing any Value from the JailTime Value object because you are storing the value in a variable. Therefore you are only subtracting from the variable.

local Time = script.Parent -- This stores the value object in a table.

while Time.Value > 0 do -- Now we check the objects "Value" property.
       Time.Value = Time.Value - 1 -- Here we subtract the value
       wait(1)
end
1 Like

What about once it hits 0, would this work?

local Time = script.Parent -- This stores the value object in a table.

while Time.Value > 0 do -- Now we check the objects "Value" property.
       Time.Value = Time.Value - 1 -- Here we subtract the value
       wait(1)
end
if Time == 0 then
local player = script.Parent.Parent.Parent
player.stats.Arrested = false
player:LoadCharacter()
player.Team = game.Teams.Neutral
end