Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Character is not being defined with :FindFirstChild()?

I attempted testing out a part that would get a player's Character with :FindFirstChild(). Here's the script:

script.Parent.Touched:Connect(function(part)
	local parent = part.Parent 
	local plr = game.Players:GetPlayerFromCharacter(parent)
	if parent and plr then 
		local char = plr:FindFirstChild("Character")
	end
end)

However, "char" returns a nil value. Why is this the case?

1 comment

0

parent of touchpart is the character, you only need to check if can get player by parent of touchpart. if you can get the player the parent is your character. And you can't use FindFirstChild to get character. only plr.Character, and if you want to get character with plr.Character you can add a check if character exists. example: local char = plr.Character if char then ... end

1 answer

✓ Accepted answer

Hi,

You can use plr.Character. Contrarily to popular belief, Character is a property of the player object, not a child. That said, you cannot use the FindFirstChild() method.

In the event that your code runs before the Character is present, you can use the CharacterAdded event as follows:

char = plr.Character or plr.CharacterAdded:wait()

For your code, however, you should be set by simply putting:

char = plr.Character

This discussion is preserved as it was. New answers, comments, and votes are closed.