I have a problem with my datastore scripts, and I can’t seem to figure out why. I’ve tried many ways to try to solve this, but none of them have worked. At this point, I’m pretty much stumped, and I can’t move on with creating my game without the use of datastores.
Here is my module to cache data:
local module = {}
local plrData = {}
function module.SetDataTable(plr,data)
plrData[plr] = data
end
function module.GetDataTable(plr)
local i = plrData[plr]
return i
end
function module.RemoveDataTable(plr)
plrData[plr] = nil
end
return module
Here is the script that runs the datastore:
local module = require(script.Parent)
local DS = game:GetService("DataStoreService"):GetDataStore("Data")
local rep = game.ReplicatedStorage
local rFunc = rep.GetData
local interval = 30
local startingData = {
-- BASIC
Money = 0,Level = 0,Experience = 0,Resets = 0,
-- STAT RELATED
["Mining Speed"] = 1,["Mining Skill"] = 0,["Mining Time"] = 0,Mine = 1, -- MINE
Inventory = 0, -- GLOBAL STATS
-- RESOURCES
Stone = 0,Iron = 0,Coal = 0,Silver = 0,Gold = 0,Sapphire = 0,Ruby = 0,Emerald = 0,Topaz = 0,Copper = 0,Tin = 0,
Steel = 0,Bronze = 0 -- ALLOYS
}
function savePlrData(plr)
DS:SetAsync(plr.UserId,game:GetService("HttpService"):JSONEncode(module:GetDataTable(plr)))
end
game.Players.PlayerAdded:Connect(function(plr)
local data = {}
for i,v in pairs(startingData) do
data[i] = v
end
local s,info = pcall(function()local i = game:GetService("HttpService"):JSONDecode(DS:GetAsync(plr.UserId))return i end)
if s and info then
for i,v in pairs(info) do
data[i] = v
end
end
module:SetDataTable(plr,data)
savePlrData(plr)
local other = Instance.new("Folder",plr)
other.Name = "Other"
local depth = Instance.new("IntValue",other)
depth.Name = "Depth"
end)
game.Players.PlayerRemoving:Connect(function(plr)
savePlrData(plr)
module:RemoveDataTable(plr)
end)
rFunc.OnServerInvoke = function(plr) -- RemoteFunction
local i = module:GetDataTable(plr)
return i
end
And here is part of the script that uses the module:
local function createOres(plr,depth)
local t = DS:GetDataTable(plr) -- DS is the module
local total,ores = module:CalculateTotal(plr,10+2*1.6^(t["Level"])) -- formula, unsure why it says "Level" is not a valid member of Player
for _,ore in pairs(ores) do
rep.SpawnOres:FireClient(plr,ore:FindFirstChildWhichIsA("BasePart"):Clone()) -- Other stuff
end
end
Any help would be appreciated!