Attempt to index nil with 'Name'

This is a script for a custom player list GUI for my game, but It does not work and outputs the error “attempt to index nil with ‘Name’” On line 14, 21, and 33. the “player” in the players.PlayerAdded:Connect(onPlayerAdded(player)) and
players.PlayerRemoving:Connect(onPlayerRemoving(player)) at the bottom the script are underlined.

ignore the icon IDs this is not finished yet.

local players = game:GetService("Players")
local replicatedStroage = game.ReplicatedStorage

local localPlayer= players.LocalPlayer

local ConsoleIcon = 6034848748
local PremiumIcon = 7132874204
local MobileIcone = 6034837811
local ComputerIcon = 6034837808
local DeveloperIcon = 6023426938
local FriendIcon = 6034287518

local function onPlayerRemoving(player)
	local PlayerBar = script.Parent.Players:FindFirstChild(player.Name)
	if PlayerBar then
		PlayerBar:Destroy()
	end
end

local function onPlayerAdded(player)
	onPlayerRemoving(player)
	local PlayerBar = replicatedStroage.PlayerBar:Clone()
	PlayerBar.Visible = true
	PlayerBar.Name = player.Name
	PlayerBar.PlayerName.text = player.Name
	PlayerBar.Parent = script.Parent.Players
	if player.MemberShipType == Enum.MembershipType.Premium then
		PlayerBar.Icon.Image = PremiumIcon
	end
	
end

players.PlayerAdded:Connect(onPlayerAdded(player))
players.PlayerRemoving:Connect(onPlayerRemoving(player))

What does this folder look like in-game? Can you post a screenshot of it in the explorer?

The “PlayerClient” script is the the one I posted
RobloxStudioBeta_ulwOG9tmu5

Remove the player arguments:

players.PlayerAdded:Connect(onPlayerAdded)
players.PlayerRemoving:Connect(onPlayerRemoving)

Where’s your player string/etc. value object? Also, Bloop might have your solution.

This did not work. the player list just does not work with out any errors in output

You should keep their suggestion, though. That was part of the issue.

What do you mean by this? (character limit lol)

On that line, you’re referencing the player’s name, but I don’t see any player names in that list. Did you get that screenshot from in-game or were you in the editor?

Wait, I just realized that you said there were no errors in the output. This means your player bar is the thing that isn’t working. The issue you posted about is solved. If you need more assistance, you should open another post.

What? the issue is not solved, my player list is still not working

Right, but your post was about the error. The issue that this post was about is solved, and continuing to talk about why your player bar itself isn’t working is irrelevant. That’s a completely separate issue. Just mark BloopDiBloop’s solution as solved and open another thread.

Try this:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local Bar = ReplicatedStorage:WaitForChild("PlayerBar")
local PlayersUI = script.Parent:WaitForChild("Players")

local ConsoleIcon = 6034848748
local PremiumIcon = 7132874204
local MobileIcone = 6034837811
local ComputerIcon = 6034837808
local DeveloperIcon = 6023426938
local FriendIcon = 6034287518

local function ClonePlayerBar(player)
	local PlayerBar = Bar:Clone()
	PlayerBar.Name = player.Name
	PlayerBar.PlayerName.Text = player.Name
	PlayerBar.Visible = true
	PlayerBar.Parent = script.Parent:WaitForChild("Players")
	
	if player.MembershipType == Enum.MembershipType.Premium then
		PlayerBar.Icon.Image = PremiumIcon
	end
end

local function onPlayerAdded(plr)
	ClonePlayerBar(plr)
end

for _, v in pairs(Players:GetChildren()) do
	ClonePlayerBar(v) -- this is for the current players that are in the game
end

local function onPlayerRemoving(player)
	for i, v in pairs(script.Parent.Players:GetChildren()) do
		if v.Name == player.Name then
			v:Destroy()
		end
	end
end

Players.PlayerAdded:Connect(onPlayerAdded)
Players.PlayerRemoving:Connect(onPlayerRemoving)
2 Likes

Yea, @BabyNinjaTime has provided the solution. Servers scripts run before the player is added, whereas local scripts run after. Hence, the PlayerAdded doesn’t fire.

1 Like

I got the Error Attempt to index nil with ‘WaitForChild’

Nvm, ReplicatedStorage was misspelled, it all works

1 Like