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

How to get the player from a server script?

Asked by 3 years ago

So basically I am trying to get the player from a server script but I just cannot get it. Does anyone know how to do this?

3 answers

Log in to vote
2
Answered by 3 years ago
Edited 3 years ago

There are multiple ways to get a single player using server. Remote events and in pairs loops. I'll show you both ways.

In Pairs Loop

for _, v in pairs(game.Players:GetPlayers()) do

end

PlayerAdded Event

game.Players.PlayerAdded:Connect(function(plr)

end)

Remote Events (Note: You should only use Remote Events IF you're gonna use GUIs and stuff, so I do not use recommend using it.)

--Local Script--
local remoteEvent = game.ReplicatedStorage.RemoteEvent

remoteEvent:FireServer(game.Players.LocalPlayer)

--Server Script--

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function()

end)

Also you can use game.Players:GetPlayerFromCharacter() on certain events such as the Part.Touched event.

0
I usually use that too. YAYAYGPPR 82 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

A serverscript doesnt work local, try it in a localscript.

0
You can get a player instance from the server LeHelary 142 — 3y
Log in to vote
-1
Answered by 3 years ago
game.Players.LocalPlayer
0
That would not work as it would return nil. Dovydas1118 1495 — 3y

Answer this question