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

Is there a way to make a loop run infinitely while allowing other strings and functions to run?

function positionBlock()
	xcoord = math.random(138.5,252.5)
	ycoord = math.random(-19,-18.5)
	zcoord = math.random(-252.5,-137.5)
	luis.Position=Vector3.new(xcoord,ycoord,zcoord)
end
luis = Instance.new("Part",workspace)
luis.Size=Vector3.new(5,1,5)
luis.TopSurface = 0
luis.BottomSurface = 0
luis.Touched:Connect(function(thingTouchingBlock)
	if thingTouchingBlock.Parent:FindFirstChild("Humanoid")then
		print("Block touched by ", thingTouchingBlock.Name) 
		positionBlock()
	end
end)
positionBlock()

so this script is making a random button appear, but I want the button to change to a random color every .3 seconds, but if I try to add a loop it would halt everything else. I want it to keep changing colors and not just change color when you touch it. plz help thanks!

1 answer

✓ Accepted answer

This can be done by coroutines.

local coroutine = coroutine.wrap(function()
	for i = 1,10 do
		print("AAAA but it's letting the next loop run at the same time")
	end
end)
coroutine()
for i = 1,10 do
	print("AAAA")
end

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