For i,v in pairs not workings with while true do script

I wanted to make a coin system, where every part in a folder would rotate.
I don’t want to add a script to every coin, as that would be laggy, with all the coins I plan to add.
The only part that’s moving is the first item in the folder.

I looked for related topics but I couldn’t find any. Does anybody know a way to bypass this?

this is my script


this is how the folder looks
Screenshot 2024-08-27 152334
each coin should be rotating, but only the first item in the folder is.

That’s because while true do yields the loop until its done (that never happens). Idk how advanced you are but you should place it in a new thread.

Add this in your loop:

task.spwan(function()
 --While true code in here
end) 
1 Like

The reason is it not working is because you are calling the while true loop while the for loop is still running. Because of this, when the for loop ends, the while true will as well. To fix this, I have simply placed the for loop outside of the while loop, in its own function, and I call it when I want it to rotate.

local function RotateCoins(ContentFolder: Folder)
	for _, Coinpart in pairs(ContentFolder:GetChildren()) do
		if Coinpart:IsA("BasePart") then
			Coinpart.Orientation += Vector3.new(0, 5, 0)
		end
	end
end

while task.wait(0.05) do -- // Change the time to your likings
	RotateCoins(script.Parent)
end
1 Like

Also in addition to everything else I don’t recommend using pairs anymore. for k, v in {1,2,3} works better and is probably quicker.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.