Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why does my script keep making studio crash?

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"

2 comments

0

Because the 'repeat' loop does not have a 'wait' inside it; When a 'loop' does not have a wait, it attempts to run the chunk infinitely at once, thus the crashing; it is running too fast that it crashes the Client (or in this case, Your Studio).

0

I did that, and it doesnt change the value now.

2 answers

Your repeat loop is probably going forever.

While the while loop has waits 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 

1 comment

0

Players.Player.PlayerGui.XPTab.MainFrame.XpBar.Text.LocalSc:1: bad argument #2 to 'max' (number expected, got userdata)

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'))

This discussion is preserved as it was. New answers, comments, and votes are closed.