Destructible street lamps script not working after regenerating

Hi,

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:

image

This is the street light model before being hit:

image

and this is the street light model after regenerating:

image

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?

Thank you in advance!

Is only connected to the first lamp made

I recommend assigning the event to a variable

local LampTouched = part.Touched:Connect(function(hit) 

Then reassigning the event to the new lamps parts

both :remove() and :clone() are deprecated

lamp:Destroy()

lamp = Backup:Clone()

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.

I’m not sure this helps but here is the place where this problem can be replicated:
https://www.roblox.com/games/6754219033/Destructible-Street-Lamps

You haven’t connected any event to the new lamps created as far as I can see

lamp = Backup:Clone()

You cloned the lamp but never recreate and connect the .Touched event

Sorry, I’m not very experienced with scripting. How could I do that?

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… :thinking:
Your loops within a loop made the process difficult this isn’t a solution I need to give this more thought

In this line

for _,lamp in pairs(script.Parent:GetChildren()) do

What is script.Parent

script.Parent is the folder in which holds all the street lamp models.

image

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.

Would it help if I gave you a place file containing all of this? Or is that asking for too much?

I know multiple solutions its just picking the best one for you to understand cause at the end of the day i want to help.

The most basic easiest thing to do is to take lamptouch() function place it into a script and have it added to new lamps everytime you replace one

LampTouch() Function
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)

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

2 Likes

I’ll do that, thank you so much for your continued help! :smiley:

1 Like