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
@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
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
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).
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.
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.
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
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