Attempt to index local ShopData (a nil value) error

I’m storing shop data in a ModuleScript, and here are some locals:

local Lighting = game:GetService("Lighting")
local ShopItems = Lighting.Pets
local player = game.Players.LocalPlayer
local ShopData = game:GetService("ServerScriptService"):FindFirstChild("ShopData")
local PetsData = ShopData["Pets"]

Well, there’s an error spawning in the line 5 (i think it’s 4)

[14:10:51.585 - Players.ConejinAlt.PlayerGui.Menu.PetsFrame.CheckSaleItems:5: attempt to index local 'ShopData' (a nil value)]
14:10:51.587 - Stack Begin
[14:10:51.588 - Script 'Players.Conejin_Alt.PlayerGui.Menu.PetsFrame.CheckSaleItems', Line 5]
14:10:51.590 - Stack End

ShopData is the Module which stores the shop data (prices, names, etc.)

return {

["Pets"] = {	
	["SlotExample"] = {
		["Name"] = "Name";	
		["Cost"] = 0;
		["Currency"] = "Nubits";
		["Rarity"] = 0;
		["ImageId"] = 0;
		["Info"] = "Description"
	};
   };
}

This is an example of how a pet’s data is stored. I don’t know why this error is spawning. Could you help me? Thanks :+1:

I don’t believe you can get the localPlayer in a module script.

Maybe try passing it in through the functions.

The contents of ServerScriptService don’t replicate to the client. You’ll want to store it in ReplicatedStorage if you want it to be accessible from the client as well.

Also, this should probably be wrapped in a require() if you actually want to get the contents of the module.

3 Likes

ShopData is a module script.

The script i’m showing is a localscript.

Hello there,

Client haven’t the permission to access to the ServerScriptService.
There is what you should do :

  • Move your moduleScript into the ReplicatedStorage(Since this one can be used from the client)
  • In your localscript, just modify it like that :
local Lighting = game:GetService("Lighting")
local ShopItems = Lighting.Pets
local player = game.Players.LocalPlayer
local ShopData = require(game:GetService("ReplicatedStorage"):FindFirstChild("ShopData")) -- I added require() since you can't acces to a module without that.
local PetsData = ShopData["Pets"]

Hope it helped.

1 Like

If it helped, make sure to mark this message as a solution to help others persons that have the same problem. :smiley:

if it’s a module script then you would have to do:

local Module = require(ShopData)

then in the module you would have a function

module.Pets()
    --Stuff
end

then, once you require it, you would call the function

Module.Pets()

@VegetationBush

Well, that’s actually not needed, that’s already tested.

@RobxSoft

Sorry, but someone already responded first, don’t worry, you also helped me, and thanks for it! :slight_smile: