I'm using this script, but it makes studio crash each time. Any ideas why?
while wait() do repeat script.Parent.Value = script.Parent.Value + 1 until script.Parent.Value == 100 wait() end
I also use this script to change the text on my gui, I fixed the crashing, but now the text on the gui won't change.
local XP = script.Parent.XP.Value script.Parent.Text = XP.." /100"
Your repeat
loop is probably going forever.
While the while
loop has wait
s to let it stop, your repeat
loop doesn't.
However, adding a wait
isn't enough -- there's some other problem.
Likely, the script.Parent.Value
isn't hitting 100
exactly. Maybe it got a fractional value (e.g., it hits 100.5
) or it's bigger than 100
(e.g., 150).
In a worse case, it also might be NaN (the result of 0/0
)
In any case, it won't ever satisfy == 100
You probably want to use until script.Parent.Value >= 100
instead, to fix these.
However, a repeat
loop at all here is silly -- right now, it will always end in precisely setting it to 100
. If you want the value to just be 100, set it to 100! If you want it to be at least 100, use math.max
!
script.Parent.Value = math.max(100, script.Parent.Value) -- it will now be at least 100
Any repeating/infinite loop, especially without wait
is going to greatly affect performance. You've added a few waits in your while
loop but should also add one inside of your repeat
.
Also, you don't need two different loops to change a certain value. It can be simply and much more efficiently accomplished with just one.
local change = script.Parent local iterator = 1 repeat wait(1) iterator = iterator + 1 change.Value = change.Value + 1 until iterator == 100
For your second snippet, you shouldn't refer to an actual Value
property's value when setting a variable.
local XP = script.Parent.XP script.Parent.Text = tostring((XP.Value..' /100'))