Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Character is not being defined with :FindFirstChild()?

Asked by 4 years ago

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?

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 yHasteeD 1819 — 4y

1 answer

Log in to vote
2
Answered by
Thetacah 712 Moderation Voter
4 years ago
Edited 4 years ago

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
Ad

Answer this question