Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Can LocalPlayer be used in a ModuleScript?

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

✓ 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

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

0

Thx

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.

3 comments

0

reasonable.

0

This is factually incorrect. Client-side module scripts can use local player

0

Correct^ @Dandystan

This discussion is preserved as it was. New answers, comments, and votes are closed.