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

Why Doesn't This For Loop Break When Told To On Roblox Studio? On BrickColor.Random

Sorry about the titile, it keeps saying "Your title should be specific! Describe your problem concisely." its so annoying

Hello guys, i am having a problem, I am doing a brickcolor.random loop i want it to stop at "Slime green" but how ever it does not I did repeat until here is my code

local part = script.Parent

while true do

repeat

part.BrickColor = BrickColor.Random()

wait(1)

until part.BrickColor == ("Slime green")

break

end

any help will be appreciated, i am trying to learn how to code roblox lua but this is really irritating, no error code at all. Thanks.

1 comment

0

Edit:- code block

1 answer

✓ Accepted answer

Instead of relying on a messy repeat loop, you could just use a simple if-statement to compare the current BrickColor.

Another issue is that you're comparing the BrickColor property to a string. The proper usage when comparing would be:

part.BrickColor == BrickColor.new('Slime green')

BrickColor property requires a BrickColor data type.


With all this said the finished product should be:


local part = script.Parent

local brickColor = BrickColor.new('Slime green') 

while true do
	if part.BrickColor == brickColor then
		print('got color')
		break
	end
	
	part.BrickColor = BrickColor.Random()
	
	wait()
end

Let me know if I missed anything or you need anything explained.

2 comments

0

My site has " in the code when the preview shows it should be " :thonk:

0

Thank you a lot appreciate it.

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