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

How can I find a player from his character?

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

2 answers

✓ Accepted answer

You'd need to use the GetPlayerFromCharacter method.

Here's an example..

p = game.Players:GetPlayerFromCharacter(game.Workspace.Noob)

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)

2 comments

0

so much complex, and no need for it, you could even use a normal script at workspace and find it without using GetPlayerFromCharacter.

0

I wrote this up late at night, got very bored so typed it.

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