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

Why is :Destroy() not working with GetChildren() ?

Hello guys, I am trying to make a LocalScript that will delete all GUis in a player's PlayerGUi folder except for some that have a certain name. The issue is I keep getting :Destroy() is a nil value because I am using GetChildren which gets it a table and apparently Destroy does not work with tables?

Here is my script

local repStorage = game:GetService("ReplicatedStorage")
local event = repStorage:WaitForChild("Died") 
local player = game.Players.LocalPlayer

event.OnClientEvent:connect(function()
	   	if player.PlayerGui:GetChildren().Name ~= "Shop" then
       		player.PlayerGui:GetChildren():Destroy()    	
    	end

end)
	

Now I have made a post similar to this before, and the solution was to do something like this,


	local guis = player.PlayerGui:GetChildren()
	for _, v in pairs(guis) do
   		if v.Name ~= "Shop"  then
       		v:Destroy()    	
    	end
	end

However, this time I am in a LocalScript...would it be silly or bad practice to do for _, v in pairs in a LocalScript?

2 comments

0

If you have FE enabled, you'll need a remote event to do the for loop from the server.

1 answer

✓ Accepted answer

...You have to use a for loop.

The way you use Lua doesn't change in the slightest between LocalScripts and Scripts.

:GetChildren() produces a list. Lists don't have .Names nor can list be :Destroy()ed. You mean to do this chunk of code for each child, which calls for a for loop.

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