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

What does :GetPlayerFromCharacter() mean?

Could someone explain what :GetPlayerFromCharacter() mean in plain English i am new to scripting however i know c++ and java please include examples and when someone would use this function

Thank you!

1 answer

✓ Accepted answer

This answer's to be used as a reference

The GetPlayerFromCharacter function's pretty self explanatory; it's a function that returns a player from their character. :p This can useful for certain things, such as checking whether a character is a player's. Here's example with the Touched event;

Part.Touched:Connect(function(ObjThatHit)
	-- The touched event listens for when an object (or a part) touches it, and when it does it returns it.
	if ObjThatHit.Parent then
	-- Checks whether the object's parent was changed to false (this can happen with bullets).
		local IsPlayer = game.Players:GetPlayerFromCharacter(ObjThatHit.Parent)
		-- The variable to retrieve the player, if it was a player.
		print(IsPlayer and 'Is a player' or 'Isn\'t a player!')
		-- Prints whether what touched it was a player or not.
	end
end)

Some things to note when using it;

  1. It's a function of the Players service, so you'll need to call it from it. Players:GetPlayerFromCharacter
  2. It requires the model for the player, thus the GetPlayerFromCharacter(Model)
  3. If it wasn't a player that was given to it, it'll return nil.

Stuff touched on

  1. GetPlayerFromCharacter - Returns a player if what was given to it was a player's character.
  2. Touched - An event that listens for when something touches the part it's connected to.

If you have any more questions, feel free to let me know. :) I hope this helped! :D

4 comments

1

still confused why would someone use this function and thank you for helping!

0

One use could be to check whether a NPC touched a shop brick. And yw. :) If this answered your question, do you mind accepting it please?

0

you use it when you don't have reference to the player but have reference to the character.

0

Tyvm for accepting! ^^

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