Parent:Destroy() not working

making an admin GUI, I tried to make It so if the player is not called ‘twichman123’ then the gui is destroyed using:

Parent:Destroy()

then I tried changing the name to ROBLOX, and it still didn’t remove the gui

here is the full script:

game.Players.PlayerAdded:Connect(function(player)
if player.Name == 'twichman123' then
    else
	script.Parent:Destroy()
    end
1 Like

Assuming this is the whole script, you haven’t closed the PlayerAdded connection. Try this:

game.Players.PlayerAdded:Connect(function(player)
    if player.Name == 'twichman123' then
        -- Run Code
    else
	    script.Parent:Destroy()
    end
end)

Edit: It looks like this is being run in a server script as you are using PlayerAdded. You should be controlling UI from the client in a local script. For that, just use

game:GetService("Players").LocalPlayer
1 Like

Like @Increated said you can get the local player directly from the client side.

Also, if your admin system works by firing remotes from a gui then be sure to validate the player when the event is received on the server side. Always assume that any player can fire any remote with any arguments, regardless of whether they have the gui or not.

okay, thanks, but I don’t get what you mean when you say

Using the PlayerAdded event on a local script isn’t necessary if the script is only for one player.
game:GetService("Players").LocalPlayer gets the player who is currently running the local script.

So your script should look something like:

local player = game:GetService("Players").LocalPlayer
if (player.Name ~= "twichman123") then
script.Parent:Destroy()
end
1 Like

I just get 16:57:13.705 - Players.twichman123.PlayerGui.AdminPanel.Script:2: attempt to index nil with ‘Name’

You’re using PlayerAdded in a Service that is going to replicate its children to the client, i.e. the StarterGui. Due to this, the script would only fire when another player joins the server. To be honets, I doubt it would fire, as I’m not sure PlayerAdded works on the client side.

Delete that script, and put this code in a LocalScript.
(Not going to post, @Blokav already did it)

2 Likes

where do I put said script? just be needing 30 chars

You put the LocalScript as a child of the ScreenGui you’re attempting to destroy.

Put it in the same place you normally had it. Just use a LocalScript instead of a regular Script.

1 Like