How can I get the 1 .Touched event to look for every door in game, both preplaced doors that I have placed in studio manually, as well as doors that get placed in during game, as well as removed. because I can’t just do
for _, v in pairs(AllDoors) do
v.Touched:Connect(function(hit)
end)
end
As that wont detect new doors that have been added, or been removed
Just create the connections table, then add a check on the CheckDoorsTouch() function: if the door isn’t found on the connections table then connect a .Touched event to it and add it to the table, otherwise just skip it and continue the iteration, this way you can call the function as many times as you want without creating multiple Touched events for every element on the “Teleports” table. When a door is removed (Or your system’s player removing function is called) search for it on the connections table and remove it by setting it to nil.
Even seperating the touched function from seperate loops still doesn’t work The walk to only occurs like 1 in every 10 shots
for _, v in pairs(Buildings:GetChildren()) do
if v:FindFirstChild('Door') then
table.insert(AllDoors, v.Door)
end
v.PrimaryPart.Touched:Connect(function(hit)
Teleport(hit, v) -- hit, teleportFrom v.Parent, teleportTo Rooms
end)
end
for _, v in pairs(Rooms:GetChildren()) do
if v:FindFirstChild('Door') then
table.insert(AllDoors, v.Door)
end
v.PrimaryPart.Touched:Connect(function(hit)
Teleport(hit, v) -- hit, teleportFrom v.Parent, teleportTo Rooms
end)
end