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

What is the difference between .Touched argument and GetPlayerFromCharacter?

Asked by 8 years ago
script.Parent.Touched:connect(function(Hit) -- I know Hit is what ever hits the script's parent
-- But can you explain what Hit is really reffering to like the Player's character

end)
GetPlayerFromCharacter() -- idk what this reffers to, like the player's character or am i wrong?

For about one day ago I asked on why my script didn't work.

Someone said that I kept reffering to the Character and yeah i dont remember all.

But can someone explain the difference between .Touched Argument and GetPlayerFromCharacter?

Yes it's a stupid question but I need to know :D!

Ty!

1 answer

Log in to vote
1
Answered by 8 years ago

When Touched fires, it fires with the argument of the single Part that touched it. This means that it could be for example a part of a tool, the Baseplate, a stray bullet, or even your character's leg.

In the event that it's something from your character, hit.Parent will be your character itself. That's where Players.GetPlayerFromCharacter comes in, because it allows you to get the Player that Character belongs to. In this case, you can do

local PlayerWhoTouched = game.Players:GetPlayerFromCharacter(hit.Parent)

Which tells you primarily:

  • If a Player touched, because PlayerWhoTouched will be nil if one didn't.
  • Who the Player that touched is.

From here, you can work with things like their statistics (On the leaderboard), their GUIs, their Backpack and more.

0
Another very easy way to look at it physically is to do print(hit), print(hit.Parent) and so on. In general, printing unknown variables is a good practice for debugging and learning MBacon15 97 — 8y
Ad

Answer this question