I still don't understand what is ":GetPlayerFromCharacter" function mean?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to understand what does the function “GetPlayerFromCharacter” means?

  2. What is the issue? Include screenshots / videos if possible!
    The issue is I can’t seem to understand what it means. Possibly you might wanna explain it differently that I can hopefully understand.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have read many topics about this but still don’t understand. The topics I read was in (Scripting Helpers.org)
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

-- This is an example Lua code block

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

11 Likes

The key concept here is knowing that “Player” and “Character” are two separate things.

Player you can think of as being your account itself.
Character you can think of as being the visual representation of you in game (Your avatar)

GetPlayerFromCharacter will goto the visual representation in game and then say “Who is in charge of this guy?” The person in charge of them is the Player.

26 Likes

It simply attempts to get a player from a character model. The function returns nil if it could not get a player from the provided instance.

This is especially useful when listening for the Touched event; which fires when a part is touched by another. In many cases you might want a player to touch this, maybe because it gives them a power up or something.

-- Example script inside a part
local part = script.Parent
local Players = game:GetService("Players")

part.Touched:Connect(function(other_part)
    local player = Players:GetPlayerFromCharacter(other_part.Parent)

    if player then -- without this check we would be attempting to index nil!
        player.Character.Humanoid.WalkSpeed = 32
    end
end)
36 Likes

Love this amazing I can visualise what you just said in m own head

5 Likes