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

How to get player from server script?

So im trying to figure out how to get the player with a server script my game is a solo game so i feel like looping through all the players constantly would be useless is there a better way?

my attempt

local plr = game.Players:WaitForChild("Player")
print(plr.Name)

this waits forever until it yields ;'/

1 comment

0

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

3 answers

✓ Accepted answer

Try this. I haven’t tested it yet.

Good luck.


Players = game:GetService("Players")
for i, player in pairs(Players:GetPlayers()) do
    print(player.Name)
end

To get a player you do:

game.Players.PlayerAdded:Connect(function(player) —gets player
       —code
end)

2 comments

0

That could work if it is a solo game. If you want it to print all players at the same time you would need to use a different script.

0

In addition, it only prints when a player joins.

If you know the name of the player you're going to be looking for just do

for i, v in pairs(game.Players:GetChildren()) do
	if v.Name == "NameOfWhoYouWant" then
       --Code you want to use

		break --So it stops since you already found who you want     
	end
end

4 comments

0

Good job, but I don’t recommend adding code 2.

0

Good job, but I don’t recommend adding code 2.

0

Whats code 2?

0

line 2

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