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

Freezing error with certain code... Help?

I am trying to make a HUD that changes depending on how many conditions are met, but each time I try to make it change in Studio's Play Mode (by editing the value's String in the Explorer), the game freezes forever...

Code :

-- All variables are correctly defined above in the script. If you need them, comment the question with the request, and I will edit this code block.

while true do
	if main.Value == "" and sub.Value == "" then
		print("main & sub are empty")
		pBG.Position, hBG.Position = pos[1], pos[2]
		mtBG.Visible, mA.Visible, mBG.Visible = false, false, false
		stBG.Visible, sA.Visible, sBG.Visible = false, false, false
		wait(1)
	elseif not main.Value == "" and sub.Value == "" then
		print("only sub is empty")
		pBG.Position, hBG.Position = pos[4], pos[5]
		mtBG.Position, mA.Position, mBG.Position = pos[1], pos[2], pos[3]
		mtBG.Visible, mA.Visible, mBG.Visible = true, true, true
		stBG.Visible, sA.Visible, sBG.Visible = false, false, false
		wait(1)
	elseif not main.Value == "" and not sub.Value == "" then
		print("nothing is empty")
		pBG.Position, hBG.Position = pos[7], pos[8]
		mtBG.Position, mA.Position, mBG.Position = pos[4], pos[5], pos[6]
		stBG.Position, sA.Position, sBG.Position = pos[1], pos[2], pos[3]
		mtBG.Visible, mA.Visible, mBG.Visible = true, true, true
		stBG.Visible, sA.Visible, sBG.Visible = true, true, true
		wait(1)
	end
end

The Studio's Output window does not give any error, because the game freezes as soon as another value check is ran.

Any help will be appreciated!

TheArmoredReaper

1 comment

0

I'm not going to post this as an answer, as I'm not sure what the problem is, but did you try adding "else wait(1)" after the last elseif?

2 answers

✓ Accepted answer

Your problem is being caused by condition ordering/misuse. Let's take a look at two pieces of code on lines 10 and 17:not main.Value == "" and not sub.Value == "". The not operator takes precedence over your equals operator, which causes main.Value and sub.Value to be false. So what lua sees this as is false == "" which will always fail. You could either correct this by using parentheses to order how the operators work not (main.Value == "") or use the not equals operator main.Value ~= "".

Also it would be better to have your wait function call outside of your if statement, since it is called for each outcome having it within is just redundancy.

1 comment

0

Thank you! This solved my problem!

Pyrondon is correct. The only reason a while loop would freeze, is if it constantly loops without waiting. After your last "elseif", put

else wait() -- or wait(1)

As long as the script has a 'time to breathe', it won't freeze.

2 comments

0

What if I instead remove the waits in every statement, and then replace "while true do" with "while wait(1) do"?

0

That should work as well.

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