PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience
Collapse Sidebar

GetPlayerFromCharacter

This function returns the Player associated with the given Player/Character, or nil if one cannot be found. It is equivalent to the following function:

local function getPlayerFromCharacter(character)
	for _, player in pairs(game:GetService("Players"):GetPlayers()) do
		if player.Character == character then
			return player
		end
	end
end

This method is often used when some event in player’s character fires (such as their Humanoid Humanoid/Died|dying). Such an event might not directly reference the Player object, but this method provides easy access. The inverse of this function can be described as getting the Character of a Player. To do this, simply access the Character property.

Parameters

Name Type Default Description

A character instance that you want to get the player from.

Returns

Return Type Summary

A player instance corresponding to the character passed as input.


Code Samples


Player Part Touch Detection

When pasted into a Script within a BasePart|Part, this code sample detects when a Player touches the part and prints their name.


Players:GetPlayerFromCharacter

local character = game.Workspace.Player
local player = game.Players:GetPlayerFromCharacter(character)
 
if player then
    print("Player is " .. player.Name)
else
    print("Player doesn't exist!")
end