How do I access Local Player in a Server Script?

I’m creating a script where I need to get something like the LocalPlayer as you would have it in a Local Script. However I need to do it in a server script.

Don’t know if this is really accurate but I normally just use

game.Players.PlayerAdded:Connect(function(plyr)
	
end)

You can use a remote event, but what exactly are you trying to call LocalPlayer in the server for?

That checks for when a player is added(I think)

The LocalPlayer is not accessible via server scripts because none of the players in the game are running that script locally.

The most common way of referring to a player via a server script would be like the example that @pinvlol provided. Along with that, here are several examples of how you could refer to one of the players in the game, depending on your use case:

Example #1 (PlayerAdded event)

local Players = game:GetService("Players") -- Defines the Players Service

Players.PlayerAdded:Connect(function(playerThatJoined) -- Listening for the "PlayerAdded" event of the Players service to fire, which runs a function whenever a player joins. "playerThatJoined" will refer to whoever joined the game.
    print(playerThatJoined.Name.." has joined the game!")
end)

Example #2 (Touched event)

local Players = game:GetService("Players")
local part = script.Parent

part.Touched:Connect(function(otherPart)
    local modelCheck = otherPart:FindFirstAncestorOfClass("Model")
    local player = Players:GetPlayerFromCharacter(modelCheck)

    if modelCheck and player then
        print(player.Name.." touched the part!")
    end
end)

Example #3 (Triggered event)

local ProximityPrompt = script.Parent

ProximityPrompt.Triggered:Connect(function(player)
    print(player.Name.." activated the ProximityPrompt!")
end)

There are plenty of other examples, but these are just a few of the common ones. I would recommend looking through the Roblox Creator Hub Documentation to learn more depending on when you need to refer to a player in the game.

thx but I was trying to use it for something else, I found a video on YT and they did something like this for i = 1, #plrs do if plrs[i].Character ~= nil then

I dont really know what it is tho

Im making a speed simulator and i gotta change their walkspeed I saw somebody do some for i = 1, #plrs do and hten they used plrs[i] but idk what it is

It sounds like that example would loop through a table / list of players and then check if their Character model exists.


Here’s a formatted version of the code you provided so it’s easier to read:

for i = 1, #plrs do
    if plrs[i].Character ~= nil then

    end
end

Now, I’ll explain what each section of that code does:

  • “plrs” is probably referring to a table / a list of all the players in the game. For simplicity sake, I’ll probably continue referring to a table as a “list” in this context. Examples of that include:
-- Example #1
local Players = game:GetService("Players")
local plrs = plrs:GetPlayers() -- This creates a table / list that says who is in the game at the time this line of code ran

or, maybe it was a table that was created manually to only include specific players:

-- Example #2
local Players = game:GetService("Players")
local plrs = {} -- Manually updated table that contains specific players in the game

local ProximityPrompt = script.Parent
ProximityPrompt.Triggered:Connect(function(player)
    local playerCheck = table.find(plrs, player) -- Looking through the "plrs" table, checks if the current player that activated the ProximityPrompt is already in that list

    if not playerCheck then -- If it doesn't find the player in the table
        table.insert(plrs, player) -- Then it adds the player to that table
    end
end)

The for i = 1, #plrs do is creating a for loop that looks through everything in the table / list, one-by-one.

  • “i” is a placeholder that will tell you how many items you’ve looked at in the loop.

  • “1” is where the loop starts, which in this case, means that it will start from the first thing it finds in the table and not skip anything that might’ve been before it.

  • #plrs” is where the loop ends. This is talking about the table called “plrs” and the # operator tells you how many things are in that list. In this case, it makes sure the loop will continue running until it goes through everything included in that list.


The if plrs[i] section of the code is looking through the “plrs” table at the current iteration of the loop. So if the loop is running for the first time, “i” will equal 1, and plrs[i] will become plrs[1]. This will refer to the first thing in the table, which would be the first player in the list.

From there, the code said plrs[i].Character ~= nil. Since we know that plrs[i] is talking about a player, this basically translates to player.Character ~= nil. This section of code is meant to make sure that the player’s Character exists (because if it doesn’t exist, it could be equivalent to “nil”). Since you wanted to update the walkspeed of the player’s Character, it’s important to make sure the Character exists, first.


Ideally, a revised version of that which is easier to read / understand would be this:

local Players = game:GetService("Players")

for _, player in Players:GetPlayers() do
    local Character = player.Character or player.CharacterAdded:Wait() -- Refers to the player's Character, or, if it doesn't exist yet, it waits for it to load before continuing

    if Character then -- If the player's Character exists, then...
        -- Continue
    end
end

I think they just explained it bad in the video, thank you so much for explaining this helped out a lot!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.