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

What does :GetPlayerFromCharacter() mean?

Asked by 5 years ago

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

Log in to vote
2
Answered by 5 years ago

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

1
still confused why would someone use this function and thank you for helping! Theswagger66 54 — 5y
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? TheeDeathCaster 2368 — 5y
0
you use it when you don't have reference to the player but have reference to the character. EpicMetatableMoment 1444 — 5y
0
Tyvm for accepting! ^^ TheeDeathCaster 2368 — 5y
Ad

Answer this question