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

What is the difference between .Touched argument and GetPlayerFromCharacter?

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

✓ Accepted answer

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.

1 comment

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

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