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

Can LocalPlayer be used in a ModuleScript?

Asked by 5 years ago
Edited 5 years ago

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!

2 answers

Log in to vote
1
Answered by
Mr_Unlucky 1085 Moderation Voter
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.

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)
0
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 Mr_Unlucky 1085 — 5y
0
Thx HomieFirePGN 137 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

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.

0
reasonable. HomieFirePGN 137 — 5y
0
This is factually incorrect. Client-side module scripts can use local player BenSBk 781 — 5y
0
Correct^ @Dandystan RobloxBuildingCareer 0 — 5y

Answer this question