How to update a For loop's :GetChildren() table or reset a for loop

Found very similar posts but all of them seemed to have solutions that didn’t work here
I have a single script running code when a proximity prompt is triggered in various objects so I don’t have the same script copied over and over

it works perfectly for preexisting doors but the nature of the game i’m making (procedurally generated) requires spawning new doors with the proximity prompts

Since the code only runs once it wont care about any new doors
Ive tried using .ChildAdded to start a new loop which does make a new door work but it doesn’t stop the old one meaning it runs twice for old doors and shuts them immediately off a single click
so I want to stop the loop and restart it but haven’t found a clean way to, or at least keep the original loop but have it listen to new doors
is there a way to do this or is there a better method without the loop
I would prefer to keep it as a loop if possible

local folder=script.Parent

for i,v in pairs(folder:GetChildren()) do
	if v.ClassName=="Model" then
		v:WaitForChild("prompt"):WaitForChild("ProximityPrompt").Triggered:Connect(function(a)
			--do stuff
		end)
	end
end
2 Likes

The keyword continue can reset while loops but only skips current iteration for for loops

you could use a coroutine to close a function and call a new one

2 Likes
local folder = script.Parent
local co

function loop()
    for _, v in folder:GetChildren() do
    	if v:IsA("Model") then
		    v:WaitForChild("prompt"):WaitForChild("ProximityPrompt").Triggered:Connect(function(a)
			    --do stuff
	    	end)
	    end
    end
end

function resetLoop()
    if co then coroutine.yield(co) coroutine.close(co) co = nil end
    co = coroutine.create(loop)
    coroutine.resume(co)
end

resetLoop()

folder.ChildAdded:Connect(resetLoop)

just call resetLoop() to reset it

2 Likes

You were on the right track by using .ChildAdded, but you shouldn’t respond with a loop. You can use its child parameter to get just the new door. Here’s what I’d do:

local function PrepareDoor(door)
    --// Do the ProximityPrompt connection and other setup shenanigans here
end

for i,v in pairs(PathToDoors:GetChildren()) do
    PrepareDoor(v)
end

PathToDoors.ChildAdded:Connect(PrepareDoor)

In case that last line confuses you, it’s basically a shortened version of this:

PathToDoors.ChildAdded:Connect(function(child)
    PrepareDoor(child)
end)
3 Likes

This worked out thank you
Let me see if I am reading the code correctly though, I want to know how it works

The loop in the middle just sends all preexisting doors to the prepare function
the bottom line sends new ones to it
and the function connects them separately outside of any loops
If that is how then honestly I don’t see how I didn’t think of that, smacking myself right now

2 Likes

Yep, you got it right!

character limit moment

1 Like

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