A while true do loop wont run under some functions

local path1 = Vector3.new(1,0,0.1)
local path2 = Vector3.new(1,0,0.2)
local path3 = Vector3.new(1,0,0.4)
local path4 = Vector3.new(1,0,0.8)
local path5 = Vector3.new(1,0,1)
local path6 = Vector3.new(1,0,1,1)
local path7 = Vector3.new(1,0,1.2)
local path8 = Vector3.new(1,0,1.4)
local path9 = Vector3.new(1,0,1.8)

local randompath






local debounce1 = false
function randompathchoose()
	math.randomseed(tick()%1*1e6)
    local choose = math.random(1,9)
if not debounce1 then debounce1 = true
if choose == 1 then
	randompath = path1
elseif choose == 2 then
	randompath = path2
elseif choose == 3 then
	randompath = path3
elseif choose == 4 then
	randompath = path4
elseif choose == 5 then
	randompath = path5
elseif choose == 6 then
	randompath = path6
elseif choose == 7 then
	randompath = path7
elseif choose == 8 then
	randompath = path8
elseif choose == 9 then
	randompath = path9
end
end
end


function removestorm()
	wait(45)
	script.Parent:Destroy()
end





-----------------------------------
local debounce2 = false
local randomizedsize
function spawnpartmanager()
	local copied
	local size1 = Vector3.new(1,1,1)
	local size2 = Vector3.new(1,1,2)
	local size3 = Vector3.new(1,2,2)
	local size4 = Vector3.new(2,2,2)
	local size5 = Vector3.new(1,3,1)
	local size6 = Vector3.new(1,4,1)
	
	if not debounce2 then debounce2 = true
	local parts = game.ServerStorage.BuildingParts:GetChildren()
	
for i = 1,1 do
	wait(0.1)
	if parts[i] and parts[i]:IsA("BasePart") then
		local b = math.random(#parts)
		local randomchoice = parts[b]
		local copy = randomchoice:Clone()
		copy.Parent = game.Workspace
		copy.Position = script.Parent.PrimaryPart.Position
		copied = copy
	end
	end
	end
	
	math.randomseed(tick()%1*1e6)
    local choose = math.random(1,6)
	if choose == 1 then
		copied.Size = size1
	elseif choose == 2 then
		copied.Size = size2
	elseif choose == 3 then
		copied.Size = size3
	elseif choose == 4 then
		copied.Size = size4
	elseif choose == 5 then
		copied.Size = size5
	elseif choose == 6 then
		copied.Size = size6
	end
	wait(5)
	debounce2 = false
	end
---------------------------------------





randompathchoose()
removestorm()
wait(0.1)
while true do
	wait()
	print("test print")
	script.Parent:SetPrimaryPartCFrame(CFrame.new(script.Parent.PrimaryPart.Position + randompath))
	spawnpartmanager()
	end

in a nutshell, the while true do loop at the bottom wont run. it prints “test print” once then stops completely. why does it do this?

When you call removestorm() the script is yielding the code wait(45) and not letting the script run anymore.
You should replace the code below:

function removestorm()
	wait(45)
	script.Parent:Destroy()
end

With the code below:

function removestorm()
    spawn(function() -- creates a seperate thread that doesn't yield
	    wait(45)
	    script.Parent:Destroy()
    end)
end

This will likely fix your problem, if it doesn’t please let me know.

Are you making sure to check the console for any errors? Chances are if the loop is not working you have some kind of yield which will stop the loop for a period of time (even infinitely) or an error which would cause the loop to break.

that seemed to have worked. it also fixed any other functions that do not work. thanks!

1 Like