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

How to put a while true loop in a for loop?

So I have 2 loops, I know you can't put a loop inside another because the for loop would not run until my while true loop inside the for loop stops, which is never since it is a while true loop. Here is my code:

script.Parent.MouseClick:Connect(function(player)

  

	for i =1,20 do

		local wel = Instance.new("Weld", workspace)
		local part = Instance.new("Part", workspace)
		prt = Instance.new("Part",workspace)
		prt.Material = Enum.Material.Neon
		prt.BrickColor = BrickColor.Random()
		part.CanCollide = false
		part.Transparency = 1
		print("worked")
		wel.Part0 = part
		wel.Part1 = player.Character.LowerTorso
		wel.Parent = player.Character.LowerTorso
		wel.C0 = CFrame.new(0.67,1,-0.8)
		local rocpro = Instance.new("RocketPropulsion",prt)
		rocpro.Target = part
		rocpro.MaxSpeed = 4000
		rocpro.MaxThrust = 2000
		prt.CanCollide = false
		rocpro:Fire()
		wait()
		while true do
			prt.BrickColor = BrickColor.Random()
			wait(0.1)
		end
end)

Everything else works of course, but how would I make the while true do loop that changes the brickcolor every 0.1 seconds?

2 comments

0

You literally just said that you can't embed a while loop in a for loop, so why are you even asking?

2

youre probably looking for a coroutine or maybe just add the while loop after the for loop since the for loop ends immediately anyway

1 answer

✓ Accepted answer

Just to mention it:

Q: Why can't I have a while loop in a for loop?

A: You actually can, but you would need to break the loop at some point for it to to continue with the previous loop. You can still create a new thread (using coroutines or spawn) which will run at the same time. However, if you do this, after a couple of times, the performance will decrease - and this is generally bad practice.

Now, to the solution, what you would do instead here is to create a table where you put the parts in after you create new parts, and then create a separate thread that will change the color of the parts in it after X seconds.

Here is a simple solution:

-- // Variables
--------

local changeColorParts = {};
local changeColorDelay = 0.1;


-- // Creating a single part and inserting it to the table
--------

local newPart = Instance.new("Part");
newPart.Parent = workspace;
table.insert(changeColorParts, newPart);


-- // Creating a new thread and running it
--------

coroutine.resume(coroutine.create( function ()	
    while true do
	    -- Looping through the parts in the table
		for index, part in pairs (changeColorParts) do
			-- Changing its color to something random
			part.BrickColor = BrickColor.Random();
		end
		-- Waiting .1 seconds
		wait(changeColorDelay);
	end
end)

This is pretty simple, it basically has a table called changeColorParts and all parts inserted to the table will have their colors changed every changeColorDelay seconds, which in this case is every 0.1 seconds.

1 comment

2

Thanks! This really helped!

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