I’m working on a very simple destructible street lights system and I’ve got most of it working.
It works by creating a TouchIntrest in all descending parts of all the street light models inside of the corresponding folder and when detecting a hit it unanchors all the parts of the specific lamp.
My only problem is, that this script works the first time around, but when the street lights regenerate. (into the StreetLamps folder) the TouchIntrest is no longer there and the script can no longer run.
Here is the script:
local CanKnockOver = true
for _,lamp in pairs(script.Parent:GetChildren()) do
for _,part in pairs(lamp:GetDescendants()) do
if part:IsA("MeshPart") then
part.Touched:Connect(function(hit)
local Backup = lamp:clone()
if CanKnockOver == true then
lamp["Meshes/LP1"].Anchored = false
lamp["Meshes/LP2"].Anchored = false
lamp["Meshes/LP3"].Anchored = false
lamp["Meshes/LP4"].Anchored = false
lamp["Meshes/LP1"].HitSound:Play()
CanKnockOver = false
wait(1)
lamp:remove()
lamp = Backup:clone()
lamp.Parent = game.Workspace.StreetLamps
lamp["Meshes/LP1"].Anchored = true
lamp["Meshes/LP2"].Anchored = true
lamp["Meshes/LP3"].Anchored = true
lamp["Meshes/LP4"].Anchored = true
CanKnockOver = true
end
end)
end
end
end
Here is where the script is located in the Workspace:
This is the street light model before being hit:
and this is the street light model after regenerating:
As you can see the TouchInterest doesn’t respawn with the clone. How could I make it so the script runs again and creates the TouchInterest into the newly regenerated street lamp?
I think I have changed what you and @D0RYU suggested, however, the problem is still here.
Here is the script with the changes:
local CanKnockOver = true
for _,lamp in pairs(script.Parent:GetChildren()) do
for _,part in pairs(lamp:GetDescendants()) do
if part:IsA("MeshPart") then
local LampTouched = part.Touched:Connect(function(hit)
local Backup = lamp:Clone()
if CanKnockOver == true then
lamp["Meshes/LP1"].Anchored = false
lamp["Meshes/LP2"].Anchored = false
lamp["Meshes/LP3"].Anchored = false
lamp["Meshes/LP4"].Anchored = false
lamp["Meshes/LP1"].HitSound:Play()
CanKnockOver = false
wait(1)
lamp:Destroy()
lamp = Backup:Clone()
lamp.Parent = game.Workspace.StreetLamps
lamp["Meshes/LP1"].Anchored = true
lamp["Meshes/LP2"].Anchored = true
lamp["Meshes/LP3"].Anchored = true
lamp["Meshes/LP4"].Anchored = true
CanKnockOver = true
end
end)
end
end
end
The problem being that when the cloned lamp is brought into the workspace the lamp descendants don’t have a TouchInterest making the script not work.
Okay so at the moment you loop the lamp and connect the touch event. You would just copy and paste it once again but then again it would just be very messy. I’ll show you one moment…
What you could do...
local CanKnockOver = true
for _,lamp in pairs(script.Parent:GetChildren()) do
for _,part in pairs(lamp:GetDescendants()) do
if part:IsA("MeshPart") then
part.Touched:Connect(function(hit)
local Backup = lamp:Clone()
if CanKnockOver == true then
lamp["Meshes/LP1"].Anchored = false
lamp["Meshes/LP2"].Anchored = false
lamp["Meshes/LP3"].Anchored = false
lamp["Meshes/LP4"].Anchored = false
lamp["Meshes/LP1"].HitSound:Play()
CanKnockOver = false
wait(1)
lamp:Destroy()
lamp = Backup:Clone()
for _,part in pairs(lamp:GetDescendants()) do
if part:IsA("MeshPart") then
part.Touched:Connect(function(hit)
local Backup = lamp:Clone()
if CanKnockOver == true then
lamp["Meshes/LP1"].Anchored = false
lamp["Meshes/LP2"].Anchored = false
lamp["Meshes/LP3"].Anchored = false
lamp["Meshes/LP4"].Anchored = false
lamp["Meshes/LP1"].HitSound:Play()
CanKnockOver = false
wait(1)
lamp:Destroy()
lamp = Backup:Clone()
lamp.Parent = game.Workspace.StreetLamps
lamp["Meshes/LP1"].Anchored = true
lamp["Meshes/LP2"].Anchored = true
lamp["Meshes/LP3"].Anchored = true
lamp["Meshes/LP4"].Anchored = true
CanKnockOver = true
end
end)
end
end
lamp.Parent = game.Workspace.StreetLamps
lamp["Meshes/LP1"].Anchored = true
lamp["Meshes/LP2"].Anchored = true
lamp["Meshes/LP3"].Anchored = true
lamp["Meshes/LP4"].Anchored = true
CanKnockOver = true
end
end)
end
end
end
This would only work 2 times I thinkkkkkk…
Your loops within a loop made the process difficult this isn’t a solution I need to give this more thought
The thing is right now you need to embed the .touched function within the new lamp and… and the loop of connecting to each part makes the process even more hard to solve simply.
I was about to show u an Object orient programming method using Self. and Metamethods but you said you werent really experienced i wouldn’t give you something you wouldnt understand.
The other method is to use CollectionService similar to what you were trying to achieve it has functionality that will be very useful to you with the very optimize approach your trying to achieve