✓ Accepted answer
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)
2 comments
Keep in mind the code above might be incorrect since I typed this out and did not test in-studio. If there are any errors please inform me
Thx