How to go about getting the player in a server script?

I am not sure what the best way to go about this would be, but I want to access the player from a server script. The game I am working on is a single player game, so no other players would be in the game. This is what I have been using to get the player, but the problem is that the player returns nil because the script runs before the player loads in. My question is how can I get around this and is this a decent solution to my problem or is what I am doing considered bad practice?

local Players = game:GetService("Players"):GetPlayers()
local player = Players[1]

For reference this is a script under an enemy, and I need the script to award that player with coins on the death of the enemy. Is my way of doing this a decent idea?

Technically you can just make a player added event and set a player variable to that player that joined.

Example:

Server Script (anywhere, for example your enemy)

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

There’s probably a better option somewhere though

You could do that OR you could use a remove event…

remote.OnSeverEvent:Connect(function(plr)
 --Player now equals plr
end

Yes, that’s what I originally responded with, however I edited my response because I feel that over complicates things

1 Like

Fair but in the chance that they already somewhat understand them I was letting them know that option too.

Actually there is no need to define player again here, you already have that parameter inside. (Unless you’d use it out of the PlayerAdded event).

There are multiple ways to get the localplayer from the server, what exactly are you needing?

Oops, I forgot to mention something. I was trying to avoid using the PlayerAdded event because it only fires once when the player is added, but I have this script under the enemy model which is spawned in multiple times, so the code only executes when the first enemy spawns in but not if more are spawned in.

If you have multiple enemy npcs, and you want to apply this on each of them, I would use module scripts here.(alongside a server script obviously)

What would go into the module script vs. the server script?

Ok well I think what you can do is:

local player = game.Players[1] or game.Players.PlayerAdded:Connect(function(plr)
    return plr
end)

Honestly don’t know if this even works lol

Yeah i messed around with that second option already but couldn’t figure out how to return the actual player, because all that gets returned is a Connection.

Only one answer.

No.

With what you’re trying to do it is near impossible.

actually im wrong, sorry for the false answer!!

Client module:

local module = {}
local player = game:GetService("Players").LocalPlayer

function module:GetPlayer(): Player
    return player
end

return module

Just require() this on the server and call the function. It should work.

Was afraid that might be the case. What should I try to do instead?

I have a solution (I think)

repeat task.wait(1)
until game.Players:GetPlayers()[1] ~= nil

local player = game.Players:GetPlayers()[1]

All you’re doing is indexing the first child inside of Players.

Yes because he has a single player game, so he only needs one player!

1 Like

Oh sorry, I didn’t read the post, guess I should next time!

1 Like

If you wanna do this all on the server there’s a few things I can think of one of them is to have a module script with a player variable which can then be accessed by any other script that require the module. You could also use bindable events and have a GetPlayer event which returns the player. You could also use collection service to tag the player when they join and then use GetTagged(Tag Name) (I think) which will return a table of everything with that tag this also replicates to the client if you needed the tag for anything else. Lastly I’m pretty sure as long as the player loaded you can just do game:GetService(“Players”):GetPlayers()[1]

Edit: Before doing any of these just make sure the player does exist by using PlayerAdded or repeat or something to wait for it and then you can set it to a variable or tag it

game.Players[1] would try to find a child of Players whose name is “1”, which would never exist under normal circumstances, so it would error.

I would try something like this:

local Players = game:GetService("Players")

local player = Players:GetPlayers()[1] or Players.PlayerAdded:Wait()
5 Likes