:Destroy() won't work? [FIXED]

Basically, I am trying to make a player list surface gui and it’s all great and working, yet it won’t destroy the player’s name on the player list when they leave.

game.Players.PlayerRemoving:Connect(function(Player)
  local plr = workspace.Laptop.Screen.SurfaceGui.ImageLabel.StartScreen2.ScrollingFrame
  local cal = plr:FindFirstChild(Player.Name)
  cal:Destroy()
end)

All I get is an error message in output saying Attempt to index nil with ‘Destroy’. Does anyone have any idea what is going on with this?

Edit: Here is the full script that has been edited, hopefully this provides more to go off of:

game.Players.PlayerAdded:Connect(function(Player)
local Name = Player.Name
if Name then
	local GUI = workspace.Laptop.Screen.SurfaceGui.ImageLabel.StartScreen2.ScrollingFrame.Player1:Clone()
	GUI.Parent = workspace.Laptop.Screen.SurfaceGui.ImageLabel.StartScreen2.ScrollingFrame
	GUI.TextButton.Text = Name
	GUI.Name = Name
end
end)

game.Players.PlayerRemoving:Connect(function(Player)
local plr = workspace.Laptop.Screen.SurfaceGui.ImageLabel.StartScreen2.ScrollingFrame
local cal = plr:FindFirstChild(Player.Name)
if cal then
	cal:Destroy();
end
end)

I am no longer getting the error message

Script now works.

4 Likes

When player leaves, it remove from Players service. That's why :Destroy function is nil for player.

( My bad, thought it was player service. )

2 Likes

He is not removing the player object itself from the Players though, at least it doesn’t in this code.

1 Like

Thanks for the reply but I want to remove the player’s name from the custom Player List GUI, I’m not really sure how to make this work.

1 Like

Try this:

game.Players.PlayerRemoving:Connect(function(Player)
    if Player then
       local plr = workspace.Laptop.Screen.SurfaceGui.ImageLabel.StartScreen2.ScrollingFrame
       local cal = plr:FindFirstChild(Player.Name)
       cal:Destroy()
    end
end)
1 Like

Strange. Didn’t work. I have no idea what’s going on here :man_shrugging:

1 Like
game.Players.PlayerRemoving:Connect(function(Player)
  local plr = workspace.Laptop.Screen.SurfaceGui.ImageLabel.StartScreen2.ScrollingFrame
  local cal = plr:FindFirstChild(Player.Name)
  if cal ~= nil then cal:Destroy() end
end)

If its nil then its nil… cal doesnt exist

1 Like

what you can do is add values in a folder then put it on anywhere you want and then

if the player leaves then set the player left value to the players name

then write a changed event and then if its not nil then destroy it by finding the player name’s frame first

1 Like

Huh, what the- It still isn’t working. I’m really confused. I might try testing again in a bit in case the game wasn’t updated yet.

1 Like

What’s going on here is that cal is nil because the script couldn’t find the child of the player’s name.

local cal = plr:FindFirstChild(Player.Name)
cal:Destroy()

So just check if there really is a child of the player’s name there, and then destroy it.

local cal = plr:FindFirstChild(Player.Name)
if cal then
    cal:Destroy()
end

Also, check that a child of the player’s name exists in explorer to make sure it’s really there.

1 Like
game.Players.PlayerRemoving:Connect(function(Player)
	local ScrollingFrame = workspace.Laptop.Screen.SurfaceGui.ImageLabel.StartScreen2.ScrollingFrame
	for i,v in pairs(ScrollingFrame:GetChildren()) do
		if v.PlayerName.Text == Player.Name then
			v:Destroy()
		end
	end
end)

You could use something like this to check for the player’s name in the list

1 Like

The error you get means that the object you’re referring to and want to destroy doesn’t exist. Make sure cal is an object. You can’t destroy something that doesn’t exist.

1 Like

It’s because you have to check if cal exists.

Simply add a

if cal then
    cal:Destroy();
end

and it should work.

1 Like

Huh, still doesn’t work. Literally everything here hasn’t worked and I’m getting really stressed ngl. I’m really not sure how to fix it.

Are you still getting the “Attempt to index nil with ‘Destroy’” error?

Are you sure that it’s the exact line as you showed us here?

I’d assume so. I’ve been testing in-game and it still hasn’t deleted the player’s name from the list. I’m thinking the game might not have updated yet so I’m going to use your script and try again in 5 minutes or so.

But why are you testing in-game?
You can also test in studio and the results will be the same.

(There are some exceptions of course but it will work with your issue)

You should always test in studio to save time.
For example, Roblox declines publish request if you send a lot of them or you changed literally nothing inside your game.

That’s atleast what I know.

Ok. I was testing in game because I was confused on how the simulated player test system works in studio. Can I add and remove players in test? How would I do this? This would save a lot of time I just can’t find how to do it.

What do you mean “add and remove players in test”?

There’s an option to test your game with multiple Roblox Studio windows.

Test > Clients and Servers

From there, you can pick how many ‘players’ do you want to test your game with.
(These ‘players’ will be your dummies which you can control just like a normal player)

I wouldn’t recommend setting this above 4, because your PC might crash or lag.
So be careful.

Yeah I wanna use the Clients And Servers but I am looking on how to remove and add fake players into the emulation