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

Reducing lag in a script help?

Alright so I have this script that starts when a bool changes and I want it to loop if that bool value is true, How?? This is what I have so far

while true do
c = script.Parent.LightUp:GetChildren()
	if script.Parent.ON.Value == true then	
    for i = 1, #c do
        if math.random() > 0.5 then
            c[i].BillboardGui.ImageLabel.Visible = true
        else
            c[i].BillboardGui.ImageLabel.Visible = false
end
end
	elseif script.Parent.ON.Value == false then
end
    wait(0.1) 
end

1 answer

Firstly, you should learn how to tab it correctly. It will greatly help your scripting. An example would be this:

while true do
	c = script.Parent.LightUp:GetChildren()
	if script.Parent.ON.Value == true then  
		for i = 1, #c do
			if math.random() > 0.5 then
				c[i].BillboardGui.ImageLabel.Visible = true
	        	else
				c[i].BillboardGui.ImageLabel.Visible = false
			end
		end
	elseif script.Parent.ON.Value == false then
		wait(.1) -- You also put the end for the entire branching of the if statement before wait causing line 11 and 12 to be utterly useless and causing an error.
	end
end

Next off is how to do this correctly. As of right now, "while true do" is basically a loop that will continue on forever. This is because the "while" loop checks if the condition is true or not. "While true" is a means of repeating the script over and over without it stopping, thus nothing will happen after it. This will NOT check boolean values. However, you can still use the "while" loop. Only with a bit of editing. Instead of checking if true is possible, you can check if the variable is true or not.

while script.Parent.ON.Value == true do

What this will do is check if the value of the boolean value that you have under your script parent. In order to compensate and correct this entire script, you would remove the if's and elseif's that deal with the on value. Thus delete lines 3, 11, 12, and 13. With this being the end result:

while script.Parent.ON.Value == true do
	c = script.Parent.LightUp:GetChildren()
	for i = 1, #c do
		if math.random() > 0.5 then
			c[i].BillboardGui.ImageLabel.Visible = true
		else
			c[i].BillboardGui.ImageLabel.Visible = false
		end
	end
end

Shame on you for skipping to the end if you did so, because this post will help your scripting knowledge as will as help you find your answer!

5 comments

0

"script.Parent.ON.Value == true" is kind of redundant.. since "script.Parent.ON.Value" should return true anyways.

0

Idk but, That didn't work. Currently it isn't giving an errors, So I can't really trobleshoot it.

0

Would putting a wait in the while loop work?

0

Try putting a print function in somewhere for the code (ie. Print after line 7). If it doesn't show tell me and what line you put it afterwards.

0

Nothing came up, So I think that'd be the while loop

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