Once you have a reference to the Player
object/Instance, Player.Character
can be used to reference their Character Model.
As an extra precaution, you may want to define their Character as
local Character = player.Character or player.CharacterAdded:Wait()
in case there’s a circumstance where you may need to wait for the player’s Character to respawn/load before performing further actions in the script.
Edit (November 1st, 2021): Here are some examples that might be useful!
LocalScript Example #1:
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait()
LocalScript Example #2
Additional Resource: CharacterAdded Event - Creator Documentation
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local function updateHealthBar()
-- Example function
end
local function characterRespawned(Character)
updateHealthBar()
local Humanoid = Character:WaitForChild("Humanoid")
Humanoid.HealthChanged:Connect(updateHealthBar)
end
player.CharacterAdded:Connect(characterRespawned)
if player.Character then
characterRespawned(player.Character)
end
Server Script Example #1:
Additional Resource: PlayerAdded Event - Creator Documentation
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(Character) -- The parameter of the function will refer to the player's Character in this case because the activation of the CharacterAdded event sends the Character instance to the function
print(Character.Name)
end)
end)
Server Script Example #2:
Additional Resource: TriggerEnded Event - Creator Documentation
local Part = workspace.Part
local ProximityPrompt = Part.ProximityPrompt
ProximityPrompt.TriggerEnded:Connect(function(player) -- When the ProximityPrompt has been activated, the TriggerEnded event tells the function which player activated it
local Character = player.Character or player.CharacterAdded:Wait()
end)
Server Script Example #3:
Additional Resources:
Team.PlayerAdded Documentation - Creator Documentation
Loops and Arrays Guide - Creator Documentation
Players:GetPlayers() Documentation - Creator Documentation
local Players = game:GetService("Players")
local Teams = game:GetService("Teams")
local function randomizePositions()
for _, player in ipairs(Players:GetPlayers()) do -- Loops through the Players Service which contains every player Instance in the game
local randomXCoordinate = math.random(-100, 100)
local randomYCoordinate = math.random(0, 200)
local randomZCoordinate = math.random(-75, 125)
local randomLocation = CFrame.new(randomXCoordinate, randomYCoordinate, randomZCoordinate)
local Character = player.Character or player.CharacterAdded:Wait()
Character:PivotTo(randomLocation)
end
end
local function teamJoin(Team)
Team.PlayerAdded:Connect(function()
local randomNumber = math.random(1, 100)
if randomNumber % 2 == 0 then -- If the number is divisible by 2, then...
randomizePositions() -- Activate the randomizePositions function!
end
end)
end
for _, Team in ipairs(Teams:GetTeams()) do
teamJoin(Team)
end
Teams.ChildAdded:Connect(teamJoin)
Post Edits Changelog
Edit #1 (November 1st, 2021) – Added LocalScript and Server Script examples for referencing a player’s Character.
Edit #2 (May 17th, 2022) – Updated the Roblox Developer Hub Documentation links throughout this post to the new Creator Documentation pages.
Because the Roblox Developer Hub will be superseded by Roblox’s Creator Documentation, I figured that it would be better to replace the links in this post sooner rather than later so that the main post wouldn’t have broken links in the far future.
Before
Player
- Developer Hub Documentation
Player.Character
- Developer Hub Documentation
CharacterAdded Event - Developer Hub Documentation
After
Player
- Creator Documentation
Player.Character
- Creator Documentation
CharacterAdded Event - Creator Documentation
Edit #3 (July 13th, 2022)
Added a greater variety of scripting examples (LocalScript Example #2 and what is currently Server Script Example #2 [the current Server Script Example #3 was the second one until I added the new example] in addition to referencing more relevant resources from Roblox’s Creator Documentation.