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

How do I call a LocalPlayer in a server side script?

Asked by 6 years ago

I've looked up all over the web trying to figure out how to do this. I'm either doing everything wrong (me irl haha). back to the point. I can't seem to be able to find the alternative way to call local players in a server script. Ex. Local Script game.Players.LocalPlayer What would i do for a server side script?

0
You can't...what are you trying to do exactly? Vulkarin 581 — 6y
0
you can use the parameter in PlayerAdded User#23365 30 — 6y

2 answers

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

You cannot call LocalPlayer, as it is not a function. It is a property. You can index it though.

Ancestry

The top ancestor (excluding the DataModel and the Players service) of a LocalScript is, well, the LocalPlayer. And LocalScripts run on behalf of the client. Scripts run on behalf of the server.

Why LocalPlayer is nil on the server

Think of it as your school classroom. Your desk is your desk, just like how your LocalScript is your LocalScript, it works for you. But your classroom is your classroom, and in it are your classmates as well as your teacher. Think of the classroom as the server. It has everyone in it, and cannot keep track of who in the heck LocalPlayer is! For this reason, LocalPlayer is nil on the server. The server isn't meant to do work for one specific player, but for all. Your game could have multiple players in it, and when you try accessing LocalPlayer, it can't just randomly pick a player. Code cannot read your mind. This is why it must be all typed out. So instead of doing this:

make a part inside of the workspace with color red

You must do this:

local part = Instance.new("Part")
part.BrickColor = BrickColor.new("Really red")
part.Parent = workspace 

So here are a few ways to get the player.

  1. Get it how Snazzy did
  2. ClickDetector.MouseClick
  3. RemoteEvent.OnServerEvent
  4. BasePart.Touched, getting the player by calling GetPlayerFromCharacter from Players
  5. RemoteFunction.OnServerInvoke (callback)
Ad
Log in to vote
2
Answered by 6 years ago

Hi there! Here may be the solution you're looking for?

game.Players.PlayerAdded:Connect(function(player)
    --do stuff here, the argument 'player' serves as a local player
end)

This should be as effective as local player... Let me know if you have any more questions! :D

Answer this question