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 4 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

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

this waits forever until it yields ;'/

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

3 answers

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

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
Ad
Log in to vote
1
Answered by 4 years ago

To get a player you do:

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

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
0
Good job, but I don’t recommend adding code 2. raid6n 2196 — 4y
0
Good job, but I don’t recommend adding code 2. raid6n 2196 — 4y
0
Whats code 2? I_Nev 200 — 4y
0
line 2 raid6n 2196 — 4y

Answer this question