Hello, so I am making a datastore script to save player data on a JoJo game I am working on, I’m using a module script and have API turned on.
So I’ve made a working datastore script, however it always says that I have no data, even though I’ve saved my data, why is this happening? No errors aswell.
Here’s my code, sory if it’s a bit messy, I tried my best to clean it up.
local dataStoreService = game:GetService("DataStoreService")
local myDataStore = dataStoreService:GetDataStore("DataStore1")
local DataModule = {}
local sessionData = {}
local noSave = {}
function DataModule.LoadData(player, retries)
local playerId = player.UserId
local currentRetries = 0
local success, errormsg = pcall(function()
myData = myDataStore:GetAsync(playerId)
end)
if success then
-- alright so here is the problem, here it checks if I had data saved, but it always returns no, even though I cleary saved new data
if myData then
sessionData[playerId] = myData
return true
else
-- it always returns this, even though I saved my data
sessionData[playerId] = {Stand = "Standless"}
return true
end
else
noSave[playerId] = true
end
return true
end
function DataModule.SaveData(player, retries)
local playerId = player.UserId
local currentRetries = 0
if noSave[playerId] then
return
end
if sessionData[playerId] then
local success, errormsg = pcall(function()
myDataStore:SetAsync(sessionData[playerId], playerId)
end)
if success then
return
else
repeat
local success, errormsg = pcall(function()
myDataStore:SetAsync(sessionData[playerId], playerId)
end)
currentRetries += 1
until success or currentRetries >= retries
if success then
return
else
return
end
end
end
end
function DataModule.SetData(player, dataName, newData)
local playerId = player.UserId
if sessionData[playerId] then
sessionData[playerId][dataName] = newData
end
end
game.Players.PlayerAdded:Connect(function(player)
-- loads the data, then I change it to something else so I can save it later (for testing)
DataModule.LoadData(player, 3)
DataModule.SetData(player, "Stand", "TheWorld")
end)
game.Players.PlayerRemoving:Connect(function(player)
-- saves the data that I changed above, but when loading it says I had no data even though I cleary set it in the code above
DataModule.SaveData(player, 3)
end)
return DataModule
I tried doing some solutions, but none of them worked. I saw someone saying that you can’t save a table to a datastore, but I’m pretty sure I’ve done it before, so I don’t think it’s that, and even if it is, no idea how I would save my data as a string and then load it later.