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

How can I find a player from his character?

Asked by 9 years ago

I know how to find a character from the player, but I can't do the opposite, help me please!

2 answers

Log in to vote
4
Answered by
Shawnyg 4330 Trusted Badge of Merit Snack Break Moderation Voter Community Moderator
9 years ago

You'd need to use the GetPlayerFromCharacter method.

Here's an example..

p = game.Players:GetPlayerFromCharacter(game.Workspace.Noob)
Ad
Log in to vote
-3
Answered by 9 years ago

Many ways first if we were to do it via LocalScript in StarterGui or StarterPack the script would look like this:

Player = game.Players.LocalPlayer --Sets the LocalPlayer
Character = Player.Character --Sets the Character

But this is only for LocalScripts what if we would want to run it from a Touched **event via **Script?

Hierarchy: game > Workspace > Part > Script

script.Parent.Touched:connect(function(TouchedObject) --Creates Touched event function
    Character = TouchedObject.Parent --Sets Character
    if Character then --If Character is TouchedObject.Parent then proceed
        Player = game.Players:GetPlayerFromCharacter(Character) --Gets Player from Character
    end
end)

There are also ways to do this via ClickDetector:

Hierarchy: game > Workspace > Part > ClickDetector > Script

Clicker = script.Parent --Defines ClickDetector
Clicker.MouseClick:connect(function(Player) --MouseClick event function
    Char = Player.Character --Gets Character
end)

Then there is of course the PlayerAdded/**CharacterAdded **events

game.Players.PlayerAdded:connect(function(Player) --PlayerAdded event
    Player.CharacterAdded:connect(function(Character) --Character Added event from Player
        print(Character.Name)
    end)
end)
0
so much complex, and no need for it, you could even use a normal script at workspace and find it without using GetPlayerFromCharacter. marcoantoniosantos3 200 — 9y
0
I wrote this up late at night, got very bored so typed it. BosswalrusTheCoder 88 — 9y

Answer this question