Hi, I’ve heard putting client modules in StarterPlayerScripts is the way to go. Thing is, when I tried to do game.Players.LocalPlayer.Module from a script inside PlayerGui, it says error type. Am I doing this wrong?
The modules you put in StarterPlayerScripts should appear in Players.LocalPlayer:WaitForChild('PlayerScripts')
Still says error type.(In script’s recommendation thing)
I can’t get the warning to happen, can you send the portion of your script that is causing it?
Also by script recommendation do you mean the script analysis or the script editor window itself?
The script editor window. Here’s the script!
local shopModule = game.Players.LocalPlayer:WaitForChild(“PlayerScripts”).Modules.MenuGUI.ShopSection
local ShopModule = require(shopModule)
For example if I type in:
ShopMo
The little recommendation list that pops up says
|🔹ShopModule| error-type |
|🔹shopModule|
Try this, it would probably be the least messy to save playerScripts to a variable so you don’t have to add
: typeof(game:GetService('StarterPlayer').StarterPlayerScripts)
to every line, and you should get auto complete as well
local playerScripts: typeof(game.StarterPlayer.StarterPlayerScripts) = game:GetService('Players').LocalPlayer:WaitForChild('PlayerScripts')
local shopGui = require(playerScripts.Modules.MenuGUI.ShopModule)
Still says error type for me
Oh yeah, oversight on my end. Hmm.
There are issues with the current type checker and I think this could be a side effect of that. I know they’re working on a new one but it’s going to take a while.
That being said there are two options.
If you don’t mind having no autocomplete for the module you could assign ShopModule
’s type to be any
like so:
local shopModule = game.Players.LocalPlayer:WaitForChild('PlayerScripts').Modules.MenuGUI.ShopSection
local ShopModule = require(shopModule) :: any
however the biggest issue with doing this is that you won’t get any autocomplete for the module.
If you want auto complete, I believe you would have to resort to typing out the whole path, starting from game
, inside the parentheses of the typeof operator like so:
local playerScripts = game:GetService('Players').LocalPlayer:WaitForChild('PlayerScripts')
local shopGUI: typeof(require(game.StarterPlayer.StarterPlayerScripts.Modules.MenuGUI.ShopSection)) = require(playerScripts.Modules.MenuGUI.ShopSection)
the major downside with this being that it’s very long
I tested this firsthand and can tell you that it works.
All right, thanks a lot for all the help and time you spent for me!
Also, if you don’t mind, can I ask what typeof does? All I know is it returns what the type of value something is, but I’ve never seen it in the way you’ve formatted it there.
In this context it’s a type checking method which assigns the variable’s type to be that of what is passed to it.
Fundamentally it works the same as how it does in TypeScript
You cannot access to a Player’s PlayerScripts from a Normal Script. You can only access to this container in a LocalScript.
You can see this because when you test your game and then you go to the Server Mode and you go to your Player, you will see that there’s no PlayerScripts container, or when you try to access to a PlayerScripts container from a Normal Script it will just error.
I don’t know why when you try to access to this container in a Script it stills pop up on the recomendation list
Sorry, I didn’t clarify in the title that put it in a local script instead of a default one. I’ll edit the title right now but thanks for the reply!
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.