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
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
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)
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)