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 the player from a server script?

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

✓ Accepted answer

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.

1 comment

0

I usually use that too.

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

1 comment

0

You can get a player instance from the server

game.Players.LocalPlayer

1 comment

0

That would not work as it would return nil.

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