I have a ModuleScript inside of my ServerScriptService that has a simple function that returns a dictionary from a table that’s connected to a player.
The Module:
local module = {}
local PlayerData = {}
function module.getData(plr)
return PlayerData[plr]
end
function module.createData(plr, data)
local newTable
if data then
newTable = data
else
newTable = {}
end
PlayerData[plr] = newTable
return newTable
end
These functions are called when a player joins the game from a seperate script as well.
local function onPlayerJoin(plr)
local new = EquipHandler.createData(plr, {
Primary = "something"
})
end
-- I connected this to a Players.PlayerAdded event
The thing is, when I go into Current: Server Mode and use this section of code in the command bar:
local module = require(game.ServerScriptService.Module)
local data = module.getData(game.Players["TheRealCybop"])
print("I am using a " .. data.Primary)
(Of course, this is all in one line.)
It shoots out an error, saying attempting to index nil as ‘Primary’.
I’m not sure as to why this happens, but this works perfectly fine when put into a normal Script. Any tips would help.
well the last thing i need to know is does the modue script have a return module becuase thats not shown in your script above
local module = {}
local PlayerData = {}
function module.getData(plr)
return PlayerData[plr]
end
function module.createData(plr, data)
local newTable
if data then
newTable = data
else
newTable = {}
end
PlayerData[plr] = newTable
return newTable
end
it should have
local module = {}
local PlayerData = {}
function module.getData(plr)
return PlayerData[plr]
end
function module.createData(plr, data)
local newTable
if data then
newTable = data
else
newTable = {}
end
PlayerData[plr] = newTable
return newTable
end
return module
now if this is just a copy paste error i can understand thats just the last thing i see as a problem
im going off to bead so i wont be able to respond for like 12 hours and i give you luck