On touch unanchor script, unanchors the whole game

the script below should unanchor all parts of the parent parent group. so script.parent.parent, while also emitting some effects for destruction effects. though it used to work, now it literally unanchors the whole game making it lag immensely. instead of just unanchoring one group model

local ready = true
local sound = script.Parent.Sound--Change to your sound location.
local delaytime = 5
script.Parent.Touched:Connect(function()
	if ready == true then
		ready = false
		for i, v in pairs(script.Parent.Parent:GetChildren()) do
			if not v:IsA("BasePart") then continue end
			v.Anchored = false
			local smoke = Instance.new("Smoke")
			smoke.Parent = v
			smoke.Color = Color3.new(197, 196, 199)
			smoke.Opacity = 0.25
			smoke.RiseVelocity = 6
		end
		sound:Stop()
		sound:Play()
		
		wait(delaytime) -- Amount of till making smoke dissapear
		
		for i, v in pairs(script.Parent.Parent:GetChildren()) do
			if v:IsA("BasePart") then
				v.Smoke:Destroy()
			end
		end
		script:Destroy()
		ready = true
	end
end)
1 Like

There doesn’t seem to be any issue with the code, are you sure that script.Parent.Parent is not the workspace?

1 Like

image

That’s odd, I copied your script and did you what you did, put the sound and script in a part, modeled all the parts needed and tested it, it only unachored the parts in the model,

1 Like

I believe I know why, the for loop iterates over everything one time, meaning that once all of the parts are destroyed, it’s an empty model. You can check if the model is empty, then you can disconnect the event and delete the model.

1 Like

Is that script originally parented inside of that part? Or is it added into the part through another script. If so it should be disabled before cloning it into there.

1 Like

i just went to the part and the script is here, i put it there

how do i go about doing that? hmmm

I don’t think that script is what is causing your problem. It’s likely another script.

2 Likes
if #script.Parent.Parent:GetChildren <= 0 then
event:Disconnect()
end
1 Like

just to clearify, there are multiple parts like these put together to create a building together. its basically a building and if you touch it it collapses

Look for one of the scripts, but check if they’re not parented into a part but instead a model.

1 Like

no, its the child of one of the parts of the build, the biggest part of the build

Are there more than one of these Destruction Scripts in your game? It could be that one of them is parented wrong

2 Likes

yes, there are alot, as i said, multiple to make a building together

The problem is that one of them is parented wrong. Search ‘Script’ in workspace, and see if you can find which one that is.

1 Like

Then it’s not an issue with the script, you must’ve misplaced one. Use the explorer to search for all the destruct scripts and find one that is wrongly parented

1 Like

ill search through the ones i know i put, my game has too many scripts to do it this way lol

You can filter via the name, although I’m not sure if it would work with spaced out names

1 Like

All you’d be looking for is a script placed inside of a model instead of a part.

1 Like