Don't get too aHEAD of yourselves! You still need the character! Get it?
Okay, I'll stop
If we dive into the Player object
We can see that the Player
has a property that we might like.
Character
.
The Character
property of Player
returns the model that we control whenever we're playing.
What's also awesome about this property? It's not Read - Only
, so we can change it!
But we're getting off - topic, and I'm a professional scripter, I don't go to OT(or do I?)
Character
objects also have a child, named Head
, which is the head we're looking for!
BAM IM AMAZING AT ANSWERING.
1 | game.Players.PlayerAdded:connect( function (plr) |
2 | print (plr.Character.Head.Name) |
BAM
ERRORS
Uh oh, errors!
Why is this erroring?
We are trying to access the Character
of the Player
while it might not be loaded yet! Scripts load the fastest in Servers, which is a bad thing if you're not waiting for anything because it'll cause everything to error.
How do we wait for the Character
:WaitForChild("Character")
?
No, for one, the Character is not a Child
of Player
, but rather, a property.
How then?
There's an event for the Player
named CharacterAdded
, which fires whenever the Character
of a Player
spawns!
So we can use that in out scripts!
1 | game.Players.PlayerAdded:connect( function (plr) |
2 | plr.CharacterAdded:connect( function (char) |
3 | local head = char:WaitForChild( "Head" ) |
And there we go!
What if you got the char, by a touched event?
Yes, I totally thought this up myself
The Touched
event returns a Part
1 | Part.Touched:connect( function (hit) |
If a character touches it? It simply returns a part of the character, not the character itself.
So, since it's a child of Character, we can so script.Parent!
1 | part.Touched:connect( function (hit) |
2 | if hit.Parent:FindFirstChild( "Humanoid" ) then |
3 | local head = hit.Parent:WaitForChild( "Head" ) |
pls at least give me some rep, I'm battling against @BlueTaslem here.