Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to get player from server script?

Asked by 5 years ago

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

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

this waits forever until it yields ;'/

0
local player = game.Players:GetPlayers()[1] or game.Players.PlayerAdded:Wait() strongrussianboy123 68 — 5y

3 answers

Log in to vote
0
Answered by
raid6n 2196 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

Try this. I haven’t tested it yet.

Good luck.

1Players = game:GetService("Players")
2for i, player in pairs(Players:GetPlayers()) do
3    print(player.Name)
4end
Ad
Log in to vote
1
Answered by 5 years ago

To get a player you do:

1game.Players.PlayerAdded:Connect(function(player) —gets player
2       —code
3end)
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. raid6n 2196 — 5y
0
In addition, it only prints when a player joins. raid6n 2196 — 5y
Log in to vote
1
Answered by
I_Nev 200 Moderation Voter
5 years ago

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

1for i, v in pairs(game.Players:GetChildren()) do
2    if v.Name == "NameOfWhoYouWant" then
3       --Code you want to use
4 
5        break --So it stops since you already found who you want    
6    end
7end
0
Good job, but I don’t recommend adding code 2. raid6n 2196 — 5y
0
Good job, but I don’t recommend adding code 2. raid6n 2196 — 5y
0
Whats code 2? I_Nev 200 — 5y
0
line 2 raid6n 2196 — 5y

Answer this question