Can't get character

  1. What do you want to achieve? Keep it simple and clear!

I want to get the character of the player and the head of the character. Then I want to clone a billboardGUI into the head so there can be an image on top of the players head.

  1. What is the issue? Include screenshots / videos if possible!

ServerScriptService.VerifiedScript:8: attempt to index nil with 'FindFirstChild'

I keep getting this error, though I don’t know what I did wrong.

Code

local whitelist = {"LMVM2041"}

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function()
		for i = 1, #whitelist do
			local Curr = whitelist[i];
			if player.DisplayName or player.Name == whitelist then
				local character = game:GetService("Players"):FindFirstChild("Character"):FindFirstChild("Head")
				local overheadGUI = game.ReplicatedStorage.OverheadGUI:Clone()
				overheadGUI.Parent = character
				print("Verification done for "..player.Name)
			end
		end
	end)
end)

This script is inside ServerScriptService.

You need to get the local player first. You don’t even need to bother with most of that since you already have the player defined via the .PlayerAdded event.

local Character = player:FindFirstChild("Character")

edit:

In fact since you’re also using the Player.CharacterAdded event you can just do this:

player.CharacterAdded:Connect(function(char)
 for i = 1, #whitelist do
			local Curr = whitelist[i];
			if player.DisplayName or player.Name == whitelist then
                          local head = char:FindFirstChild("Head")
1 Like

This is unnesasary as there is a character parameter passed in through the Player | Roblox Creator Documentation

player.CharacterAdded:Connect(function(character) -- Character passed in here.
		for i = 1, #whitelist do
			local Curr = whitelist[i];
			if player.DisplayName or player.Name == whitelist then
				local overheadGUI = game.ReplicatedStorage.OverheadGUI:Clone()
				overheadGUI.Parent = character.Head
				print("Verification done for "..player.Name)
			end
		end
	end)
1 Like
local whitelist = {"LMVM2041"}

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function()
		for i = 1, #whitelist do
			local Curr = whitelist[i];
			if player.DisplayName or player.Name == whitelist then
				local character = player.Character:WaitForChild("Head")
				local overheadGUI = game.ReplicatedStorage.OverheadGUI:Clone()
				overheadGUI.Parent = character
				print("Verification done for "..player.Name)
			end
		end
	end)
end)

Also it is better to use IDs because if you change name this script will not work anymore unless you change the name in the table.

This works perfectly, exactly what I needed. Thank you!

1 Like