Help with a For loop problem

I want to make a system in which when you close a door by activating a ProximityPrompt, it remains closed for 10 seconds, and then it reopens automatically (but instead of doing this, what I actually do is delete the door and generate a new one already open), for a temporary defense system

The problem is that for some reason the door is generated correctly in the previous door position, but the door that should be deleted (which is supposed to be the door that is Parent of the ProximityPrompt that I activated), is actually the door of the previous ProximityPrompt that I activated is the one that is deleted.

for i, doorPrompt in workspace.Map:FindFirstChildOfClass("Folder"):FindFirstChild("Doors"):GetDescendants() do
			if doorPrompt:IsA("ProximityPrompt") and doorPrompt.Parent.Parent.Parent:FindFirstChild("DoorType") and doorPrompt.Parent.Parent.Parent:FindFirstChild("DoorType").Value == "USABLE OPEN" then
				--local doorPrompt = instance
				if doorPrompt then
					if doorPrompt then
						doorPrompt.ActionText = "Close for " .. timeClosed .. " Seconds " .. "(" .. DoorsPrice.Value .. ")"
						doorPrompt.Triggered:Connect(function(plr)
							if plr.Coins.Value >= DoorsPrice.Value then --and canClose == true then
								canClose = false
								plr.Coins.Value -= DoorsPrice.Value
								DoorsPrice.Value = DoorsPrice.Value * 2
								doorPrompt.Enabled = false
								print("Reseting door...")
								task.wait(10)

								for i, sameDoorClone in workspace["Assets"].Doors:GetDescendants() do
									if sameDoorClone:IsA("Model") and sameDoorClone.Name == doorPrompt.Parent.Parent.Parent.Name then
										sameDoorClone.PrimaryPart.CFrame = doorPrompt.Parent.Parent.Parent.PrimaryPart.CFrame
										task.wait(1)
									end
								end
								doorPrompt.Parent.Parent.Parent:Destroy() --THIS line is the problem or something
								print("Door reset!")
								canClose = true
								--doorPrices()
								task.spawn(function()

								end)
							end
						end)
					end
				end
			end
		end

i tried with Task.Spawn, i tried changing the system completely to Workspace.Map.DescendantAdded, nothing works, i even tried to instead of destroying the model, just change its Parent to ReplicatedStorage or something so it wouldn’t be seen

I should mention that the doors already have a system so that when the ProximityPrompt is activated the door closes, that script belongs to the individual door, but I wanted to make this system general and detect all the doors on the map at the same time.

1 Like

Why not just change the collision and visibility…?

1 Like

It’s a complete model, but I’ll try to do it right now with another For loop inside

1 Like

Your code is very messy and a little hard to follow, so correct me if im wrong, after 10 seconds you loop an assets folder of doors and check if they have the same name? im confused why your assets folder is in workspace, youre also not cloning it youre just directly changing the clones cframe to the position instead of making a clone, which if you have more doors it will eventually not work on some. I’d be able to help more if you give more context

1 Like

i made your code easier to read using guard clauses as it was so hard to read

 for i, doorPrompt in workspace.Map:FindFirstChildOfClass("Folder"):FindFirstChild("Doors"):GetDescendants() do
        if not doorPrompt:IsA('ProximityPrompt') then continue end
        if not doorPrompt.Parent.Parent.Parent:FindFirstChild("DoorType") then continue end
        if not doorPrompt.Parent.Parent.Parent:FindFirstChild("DoorType").Value == "USABLE OPEN" then continue end
        if not doorPrompt then continue end
            doorPrompt.ActionText = "Close for " .. timeClosed .. " Seconds " .. "(" .. DoorsPrice.Value .. ")"
            doorPrompt.Triggered:Connect(function(plr)
            if plr.Coins.Value < DoorsPrice.Value then return end --and canClose == true then
            canClose = false
            plr.Coins.Value -= DoorsPrice.Value
            DoorsPrice.Value = DoorsPrice.Value * 2
            doorPrompt.Enabled = false
            print("Reseting door...")
            task.wait(10)

            for i, sameDoorClone in workspace["Assets"].Doors:GetDescendants() do
                if not sameDoorClone:IsA("Model") then continue end
                if not sameDoorClone.Name == doorPrompt.Parent.Parent.Parent.Name then continue end
                sameDoorClone.PrimaryPart.CFrame = doorPrompt.Parent.Parent.Parent.PrimaryPart.CFrame
                task.wait(1)
            end
            doorPrompt.Parent.Parent.Parent:Destroy() --THIS line is the problem or something
            print("Door reset!")
            canClose = true
            --doorPrices()
            task.spawn(function()

        end)
    end)
end ```
1 Like

Oh, I don’t know how I didn’t notice that, I completely forgot to change the Parent from the clone to the map (And yes, this “Assets” is supposed to be in ServerStorage, but I moved it to Workspace so I can see the objects, I think it works anyway.), I also forgot to clone the new object.

2 Likes

Forget it completely, I reworked the system completely, I don’t need this script anymore.