Hey DevForum members, hoping you had a good day.
I’m having a problem with storing data in a table inside a ModuleScript.
I used to use Values inside the player for actual data saving. I decided to move to tables in ModuleScripts to manage the data. However I noticed a problem when importing the data.
-- Module Script
local module = {}
module.Players = {}
function module:ImportUserData(Player,Data)
local UserData = {
UserId = Player.UserId,
Money = Data.Money,
["InventoryData"] = {},
["ShopData"] = {},
}
table.insert(module.Players,UserData)
end
function module:ExportUserData(Player)
local UserData
for i,v in ipairs(module.Players) do
if v.UserId == Player.UserId then
UserData = {
Money = v.Money,
["InventoryData"] = {},
["ShopData"] = {},
}
table.remove(module.Players,i)
return UserData
end
end
end
function module:GetUserData(Player)
for i,v in ipairs(module.Players) do
if v.UserId == Player.UserId then
return v
end
end
end
return module
-- Server Code on a RemoteFunction
local BaseModule = require(script.Parent)
function Test(Player)
local UserData = {
Money = 50,
}
BaseModule:ImportUserData(Player,UserData)
end
game.ReplicatedStorage.ReplicatedRemotes.LoadData.OnServerInvoke = Test
If I go into accurate solo, call the RemoteEvent from the script or the Command Bar, switch to Server, and try to get that data, it won’t exist. However, if I call the module :ImportUserData()
function directly from the command bar (on the server) , it’ll exist. I’m assuming somethings wrong with the RemoteFunction?
Any help would be appreciated.