Answered by
5 years ago Edited 5 years ago
Client-side module scripts can use LocalPlayer, like in StarterPlayerScripts and stuff. However, if they are inside stuff like ServerScriptService that'll throw an error. Normal server scripts cannot use LocalPlayer.
You can however get the LocalPlayer in scripts/module scripts using remote events, remote functions, etc. For ModuleScripts you can create a function within it that has a parameter for the player.
The code below is an example of a Module Script in server script service.
3 | function Module.GetPlayer(Player) |
The code below is an example of a local script using the module script above to give the module a player instance.
1 | local Player = game:GetService( "Players" ).LocalPlayer |
3 | require(game:GetService( "ServerScriptService" ).Module).GetPlayer(Player) |
Here's another version of the example above, however using normal scripts.
The code below is a server script.
1 | function onGiveScriptPlayer(Player) |
5 | game:GetService( "ReplicatedStorage" ).GiveScriptPlayer.OnServerEvent:Connect(onGiveScriptPlayer) |
The code below is a local script.
1 | game:GetService( "ReplicatedStorage" ).GiveScriptPlayer:FireServer(game:GetService( "Players" ).LocalPlayer) |