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

Why can't I access the Local Player through a module script?

Asked by
Soban06 410 Moderation Voter
4 years ago

I know that the local player can be accessed via local script like this:

game.Players.LocalPlayer

But I am using a module script, so I try to access the local player like this (in module script):

local Players = game:GetService("Players")

So now in order to access the LocalPlayer, I tried to do this (in module script):

local Players = game:GetService("Players")

print(Players.LocalPlayer)

But the Players.LocalPlayer just prints nil

Is it because I am using a module script and local players can't be accessed through it or is it because I am not accessing the local player properly?

And if the error is because I am using a module script, then is there any way to access the local player with another method?

Side Note:

I am trying to access the local player because I want to increase one of their leader board's stat. So I tried to do this:

Players.LocaPlayer.leaderstats.Kills.Value += 1

And this too:

Players.LocalPlayer:WaitForChild("Kills").Value += 1

But the Players.LocalPlayer is returning nil

Thanks for any help

3 answers

Log in to vote
0
Answered by 4 years ago

game.Players.LocalPlayer is indeed you. but its not the name of you its the model of you.

instead of printing game.Players.LocalPlayer you should do:

print(game.Players.LocalPlayer.Name)

and then it will print the name of your account

0
It is saying 'attempt to index nil with 'Name''. I think it is because we can't use game.Players.LocalPlayer inside a module script. Soban06 410 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

A module script will run as a local script if it is required by a local script and will run as a server script if required by a server script. If you have a module script inside ReplicatedStorage and is required by both a local script and a server script, then it actually makes two completely separate copies: one for the client and one for the server. If it's called by both the client and the server then you can treat it as no different than having two different module scripts that do not interact with one another at all. For example: if the client changes a value inside the module script, then that change is not replicated to the server. BUT the same is also true for the server; i.e. if the server changes a value inside the module script, it is not replicated to the client's version of the module script.

I don't like placing modules inside ReplicatedStorage because it can get a little confusing to keep track of (and it also exposes the server-sided code to exploiters to pick through). Since both the server and the client get a copy of the code anyway, I prefer to just have a copy of the module inside the StarterPlayerScripts and a copy in ServerScriptService. If it's the same exact code without any need to verify values and the values don't change at all (like formulas) then there's really no harm in placing the module in ReplicatedStorage.

My guess is that your module is required by a server script, so it is no different than attempting to get the local player on the server (there is no local player on the server). Instead, you should capture the player through a remote event/function.

[EDIT] Here is a link that teaches you how to use remote events/functions: Remote Functions and Events

0
Actually. This module script is 'WeaponsSystem'. Inside the Roblox's Gun's Kit. And can you post a link to somewhere which can teach me how to get the player through remote events/functions? Soban06 410 — 4y
Log in to vote
0
Answered by
Glacitron 239 Moderation Voter
4 years ago

You can just add the plr as an argument in the functions when you use that code

Answer this question