Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Destroy() script not working?

Hey there, I have a script that clones a model from ReplicatedStorage and parents it to workspace using UIS and Remote Events.

I also have another script that should remove these cloned models from the workspace. I am trying to do this by using GetChildren to get the children of workspace and then using a for loop to check if there are any objects named "Model", if there is then these models would be destroyed.

Yet, nothing is happening?

What would solve this issue? Any help would be greatly appreicated!

Here is the code:

local rs = game:GetService("ReplicatedStorage")
local event2 = rs:WaitForChild("RemoteEvent2")
local c = game.Workspace:GetChildren()

local function remove()	
	for i = 1, #c do
if c[i].Name == "Model" then
		c:Destroy()
		end
		end


event2.OnServerEvent:Connect(remove)

3 comments

0

Try publishing your game 

0

Try using workspace:GetDescendants() instead of workspace:GetChildren().

0

I have, still nothing :/

3 answers

✓ Accepted answer

Try using for i,v in pairs(c) do it might work and it's easier to use.

local rs = game:GetService("ReplicatedStorage")
local event2 = rs:WaitForChild("RemoteEvent2")
local c = game.Workspace:GetChildren()

local function remove() 
	for i, v in pairs(c) do
		if v.Name == "Model" then
			v:Destroy()
		end
	end
end

event2.OnServerEvent:Connect(remove)

Or if you wanted to see if the child is a model and not just named model then you can do this:

local rs = game:GetService("ReplicatedStorage")
local event2 = rs:WaitForChild("RemoteEvent2")
local c = game.Workspace:GetChildren()

local function remove() 
	for i, v in pairs(c) do
		if v:IsA("Model") then
			v:Destroy()
		end
	end
end

event2.OnServerEvent:Connect(remove)
local rs = game:GetService("ReplicatedStorage")
local event2 = rs:WaitForChild("RemoteEvent2")
local children = game.Workspace:GetChildren()

local function remove() 
    for i = 1, #children do
		if children[i].Name == "Model" then
			children:Destroy()
		end
	end
end

event2.OnServerEvent:Connect(remove)

You're missing an end to end off the function.

1 comment

0

Still not working sadly

The problem may be that you are attempting to destroy 'c' which holds all the children of workspace, try destroying c[i], Another note, Is the 'event2' connecting?

This discussion is preserved as it was. New answers, comments, and votes are closed.