Index nil with head

	local Head = game.Players:FindFirstChild(Guy).Character.Head

Guy is a players name btw

Thanks for help!

just do

WaitForChild("Head") ?

1 Like

now its infinite yeild possible for

local guyInGame = game.Players:FindFirstChild(Guy)
local Head = nil
if guyInGame then
Head = guyInGame.Character.Head
end

That means the character isn’t properly loaded in, there are 2 methods you could use for this.

CharacterAdded:Wait()

local Player = game.Players:FindFirstChild(PlayerName) -- the player
local Head = Player.CharacterAdded:Wait().Head -- waits until the character is added, before fetching the head

or repeat wait()

local Player = game.Players:FindFirstChild(PlayerName) -- find the player

repeat task.wait() until Player.Character -- repeats a wait until the character is not nil

local Head = Player.Character.Head -- gets the head

The player doesn’t have a character by the time your code runs. You may want to use this line when directly running code related to the character’s head, instead of defining it at script load for later use(basically define it within a scope/code block when you actually need it).

If the code isn’t in an event connection(that makes a separate thread) you may want to add a safety check to avoid breaking code:

local function getCharacterHead(username: string): MeshPart?
	local player = game.Players:FindFirstChild(Guy)
	if not player or not player.Character then return nil end
	return player.Character:FindFirstChild("Head")
end

--within your code block:
local head = getCharacterHead(Guy)
if not head then return end

Try this:

game:GetService("Players"):FindFirstChild(Guy).CharacterAdded:Connect(function(char)
    local head = char:WaitForChild("Head")
    --rest of the code bla bla bla
end)

Otherwise, if you wanna grab the head from somewhere in the game, not when they join:

local head = game.Workspace:WaitForChild(Guy).Head
1 Like
local plr = game.Players.LocalPlayer -- do this if this is a local script
local character = plr.Character or plr.CharacterAdded:Wait() -- gets the player's character directly, and if it doesn't get the character immediately, it waits for the character to be added
if character then -- if the character exists/got added
	local Head = character:FindFirstChild("Head") -- searches for the player's head
	if Head then -- if the player has the head
		-- rest of ur script lol
	end
end

Also i got no idea why would you want to search for a player using their name and stuff when you can just grab their name by either doing

local script:

local playersName = game.Players.LocalPlayer.Name

serverscript:

game.Players.PlayerAdded:Connect(function(player)
	local PlayersName = player.Name
end)

OR get the player’s name once a remoteEvent is fired:

game.ReplicatedStorage.RemoteEventName.OnServerEvent:Connect(function(player)
	local playersName = player.Name
end)