Getting player from server sided script?

I wanna know how I would get the player from a server sided script (The server size is 1). so far I’ve just been doing

local localPlayers = Players:GetPlayers() 
local player = localPlayers[1]

But that doesn’t work very well. What other ways could I get the player?

1 Like

it should work fine, as long as there is a player in the game

You can always do

local player = nil

game.Players.PlayerAdded:Connect(function(plr)
    player = plr
end)

Or of course if you have a hit detection for a character you can use:

game.Players:GetPlayerFromCharacter(char)
2 Likes

You could use FindFirstChildOfClass:
local player = game.Players:FindFirstChildOfClass("Player").

1 Like

Yes but that would get a random player not the local player plus if it does get the local player it is done nearly the exact same way op did it originally

The goal of the script is to get the one player out of a server size of one. @08Boat’s solution is completely valid in this scenario for a server size of one player.

1 Like

the poster literally said the server size was 1, wth

1 Like

My bad I didn’t see that he said it was a server size of one. Sorry. In that case you guys are correct. Now I’m wondering why is this question posted if the op can just use game.Players:GetChildren()[1] (like they said) to get the player if it’s a server size of 1 (it seems like it should work fine)

I think this works best:

local player = game:GetService("Players").PlayerAdded:Wait()

This will yield the code until the player joins

3 Likes

This is exactly what I was looking for! Thank you :grin:

in case the player is already in the game you can do this instead

local Players = game:GetService("Players")
local Player = Players:GetPlayers()[1] or Players.PlayerAdded:Wait()

Sorry for the necro bump, but Players:GetPlayers()[1] Only gets the first player of the server.

It’s a Server Size of one. Don’t worry, I said the same thing before lol: