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

How do you Reference Player in a Script?

I know you can find the player in a local script with

local Player = game.Players.LocalPlayer

But how would I got about finding it in a Regular Script?

2 answers

It depends on what information you have about the player in question.

If you have a player's character model, you can use the GetPlayerFromCharacter function, like this:

local Player = game.Players:GetPlayerFromCharacter(Character)

If you only have their name, I personally would just index Players, like this:

local Player = game.Players[Name]

If you want to get a player when they join the game, use the PlayerAdded event, like this:

game.Players.PlayerAdded:connect(function(player) end)

There is no such thing as a local player in a regular script. Regular scripts run from the server, so there is no singular player to get a reference to. Of course, you can still obtain references to individual players in a number of ways (other than by name).

You can run code for a player upon their arrival, like this:

game.Players.PlayerAdded:connect(function(player)
	-- `player` is your reference to the newly joined player.
	-- Do whatever you want with it in here.
end)

Or you can run code for all players in the server with a for loop:

for _, player in pairs(game.Players:GetPlayers()) do
	-- `player` is your reference to the player we're currently looking at.
	-- Remember that this for loop runs code for each player one-by-one, so it's not a good idea to add any delays in here without creating a new thread.
end

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