[SOLVED] For i,v loop doesn't work in localscript?

I have this localscript (more like a handler) and its supposed to loop through the parts and execute the code. It stops on the for i, v loop for some reason, and I have no idea why. Pls help me

the code:

task.wait(1.4)
local AllMonitors = game.Workspace.GeneratedConcreteTunnels:WaitForChild("Connector").MonitorFolder
local PropMonitors = AllMonitors.PropMonitors
local PositionMonitors = AllMonitors:WaitForChild("PositionMonitors")
local WorkingMonitors = AllMonitors.WorkingMonitors
local ServerMonitorTemplate = script:WaitForChild("ExampleMonitor")
local ServerScreen = script:WaitForChild("Screen")
local ServerViewPart = script:WaitForChild("ViewPart")
print("specified all variables in "..script.Name)

for i, v in pairs(PositionMonitors:GetDescendants()) do

	local MonitorTemplate = ServerMonitorTemplate:Clone()
	local Screen = ServerScreen:Clone()
	local ViewPart = ServerViewPart:Clone()
	print("cloned")
	-- cloning
	local monitorParent = Instance.new("Model")
	print("model created")
	-- model creating
	MonitorTemplate.Parent = monitorParent
	Screen.Parent = MonitorTemplate
	ViewPart.Parent = monitorParent
	monitorParent.Parent = WorkingMonitors
	print("parents set")
	-- setting parents
	local weldconstraint = Instance.new("WeldConstraint")
	local weldconstraint2 = Instance.new("WeldConstraint")
	weldconstraint.Part0 = MonitorTemplate
	weldconstraint.Part1 = Screen
	weldconstraint2.Part0 = MonitorTemplate
	weldconstraint2.Part1 = ViewPart
	print("welds created")
	-- welds
	monitorParent:PivotTo(CFrame.Angles(0,0,0))
	monitorParent:PivotTo(v.CFrame)
	ViewPart:PivotTo(ViewPart.CFrame*CFrame.new(0,0,3))
	print("pivoted")
	-- pivots
	local MonitorValue = Instance.new("NumberValue")
	MonitorValue.Name = "MonitorOrder"
	MonitorValue.Parent = monitorParent
	monitorParent.Name = "Monitor"..v.MonitorOrder.Value
	MonitorValue.Value = v.MonitorOrder.Value
	print("value set")

	local MonitorUI = script.MonitorUIFolder.UI:Clone()
	MonitorUI.Parent = MonitorTemplate
	MonitorUI.Adornee = MonitorTemplate.Screen
	print("IU cloned")

	local MonitorPrompt = script.MonitorUIFolder.MonitorProxTemplate:Clone()
	local MonitorPromptValue = MonitorValue:Clone()
	MonitorPromptValue.Parent = MonitorPrompt
	MonitorPrompt.Parent = MonitorTemplate
	MonitorPrompt.Adornee = MonitorTemplate
	print("prompt created")

end

Does it print line 9 and “cloned”?

If you have a lot of descendants, the script might be overloading and lagging, try putting a wait() at the beginning of the loop

1 Like

It doesn’t print cloned, and before I had a :GetChildren() instead of the descendants (there are only 3 parts in the folder)

Did :GetChildren() work before

You may also have accidentaly set the script to stop at the loop if you have this red circle near the line
image

Nope, there aren’t any red dots near any line.

Does your specified all variables print statement print anything?

1 Like

The first print() only prints, the rest doesn’t print and there aren’t any errors in the output

1 Like

Try putting a print statement right after for i, v in pairs do. If that doesn’t work, maybe try putting something like this in your script just to see if it’s an issue with the for loop or something else.

for _, v in pairs(PositionMonitors:GetDescendants()) do
    print(v)
end

Another thing I thought of:

print(#PositionMonitors:GetDescendants())
1 Like

Perhaps the game didn’t get to load all of the instances before the script runs. Try making task.wait() longer and see if something changes

2 Likes

I increased the task.wait() time and added a print after starting the loop but it still doesn’t print anything (might I add because I didnt state this earlier; this is a localscript inside of StarterPlayerScripts)

Make sure that PositionMonitors actually has descendants/children. If it doesn’t, then the for loop obviously won’t run.

I don’t really know why else the for loop isn’t running.

1 Like

Since all instances are loaded, that might not be the problem

2 Likes

PositionMonitors has 3 descendants.

If you remove everything in the current loop and instead try to print something, does it work?

This is quite unusual and something inside the loop might be causing the problem

1 Like

Nope, it still only prints the first print and anything inside of the loop doesn’t.

In case this is related to delays and such, perhaps you could try an event-oriented approach?

local function monitorAdded(monitor: Instance)
	print(monitor)
end

for _, v in pairs(PositionMonitors:GetDescendants()) do
	task.spawn(monitorAdded, v)
end
PositionMonitors.DescendantAdded:Connect(monitorAdded)
1 Like

Does PositionMonitors have any children? You may be looping over an empty table.

We’ll need some more info, like the file hierarchy

1 Like

First, you should be using ipairs instead of pairs in this case. Second, please share an image of the output.

If it is not printing “cloned” then the code is either hanging somewhere before that, or there are no descendants of PositionMonitors.

1 Like

I had this issue once as well. Are you sure everything is loaded when you run the loop?

You could try waiting 10 seconds instead of 1.4 to see if that’s the case.

1 Like

The problem is that the loop doesn’t start, not anything inside the loop