I'm working on a dataModule right now, and I'm a bit confused with this error
10:56:08.088 ServerScriptService.DataModule:80: attempt to index function with 'SName' - Server - DataModule:80
10:56:08.089 Stack Begin - Studio
10:56:08.089 Script 'ServerScriptService.DataModule', Line 80 - Studio - DataModule:80
10:56:08.089 Script 'ServerScriptService.PlayerData', Line 38 - Studio - PlayerData:38
10:56:08.090 Stack End - Studio
I'm trying to load player data thru a table to minimize datastore throttling, however, the only character on line 80 is a }. If anybody has a fix, please tell me, it'd be great This is what it is connected to
local saveData = {
}
The dataModule
local dataModule = {}
local DSS = game:GetService("DataStoreService")
local http = game:GetService("HttpService")
local dataStore = DSS:GetDataStore("plr-Data")
--[[
Create leaderstats
Args:
userStats (table) - Table of all stats
plr (Player) - Player to create the leaderstats for
]]
dataModule.init = function(userStats, plr)
if userStats and plr then
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = plr
for i, v in pairs(userStats) do
if v.SName and v.Type and v.StartValue >= 0 then
local stat = Instance.new(v.Type.."Value")
stat.Name = v.SName
stat.Parent = leaderstats
end
end
end
end
--[[
Load leaderstats
Args:
userStats (table) - Table of all stats
plr (Player) - Player to load the leaderstats for
Returns:
stats (table) - Entire table of the user's stats
]]
dataModule.load = function(userStats, plr)
if plr and userStats then
local saveData
local success, err = pcall(function()
local data = dataStore:GetAsync("plr-"..plr.UserId)
if data then
saveData = http:JSONDecode(data)
end
end)
if success then
if saveData then
for i, v in pairs(userStats) do
local stat = plr.leaderstats[v.NSame]
stat.Value = saveData[v.SName]
end
else
for i, v in pairs(userStats) do
local stat = plr.leaderstats[v.SName]
stat.Value = v.StartValue
end
end
end
end
end
--[[
Save leaderstats
Args:
userStats (table) - Table of all stats
plr (Player) - Player to save the leaderstats for
]]
dataModule.save = function(userStats, plr)
if plr and userStats then
local saveData = {
}
for i, v in pairs(userStats) do
local stat = plr.leaderstats[v.SName]
saveData[v.SName] = stat.Value
end
print(http:JSONEncode(saveData))
end
end
return dataModule
The server script
local dataModule = require(script.Parent:WaitForChild("DataModule",60))
-- recommended you close this
local userStats = {
["Stat1"] = {
["SName"] = "Bank",
["Type"] = "Int",
["StartValue"] = 2000
},
["Stat2"] = {
["SName"] = "Wallet",
["Type"] = "Int",
["StartValue"] = 0
},
["Stat3"] = {
["SName"] = "Jail",
["Type"] = "Int",
["StartValue"] = 0
},
["Stat4"] = {
["SName"] = "Hunger",
["Type"] = "Int",
["StartValue"] = 100
},
["Stat5"] = {
["SName"] = "Thirst",
["Type"] = "Int",
["StartValue"] = 100
}
}
game.Players.PlayerAdded:Connect(function(plr)
dataModule.init(userStats, plr)
dataModule.load(userStats, plr)
end)
game.Players.PlayerRemoving:Connect(function(plr)
dataModule:save(userStats, plr)
end)
