Hi, I know that game.Players.LocalPlayer
can only be used in a LocalScript. Recently I've been working on some ModuleScripts and I've came across this question when I was working on it. I would like to know if game.Players.LocalPlayer
can be used in a ModuleScript and also, if it can be used in Normal Scripts.
Thanks for helping!
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.
local Module = {} function Module.GetPlayer(Player) print(Player.Name) end return Module
The code below is an example of a local script using the module script above to give the module a player instance.
local Player = game:GetService("Players").LocalPlayer 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.
function onGiveScriptPlayer(Player) print(Player.Name) end game:GetService("ReplicatedStorage").GiveScriptPlayer.OnServerEvent:Connect(onGiveScriptPlayer)
The code below is a local script.
game:GetService("ReplicatedStorage").GiveScriptPlayer:FireServer(game:GetService("Players").LocalPlayer)
local player can only be used in a localscript and not any script else. This is because that the localscript is just local while modulescript is much more like a normal script.