Hello, recently I wrote some very primitive code just to make a simple datastore for my RP game, saving works perfectly fine but once I rejoin the server it says it couldn’t retrieve any Data, sorry if some of the strings are in Portuguese but I’m sure you guys can understand everything.
--< Variables >--
local datastoreservice = game:GetService('DataStoreService')
local statsstore = datastoreservice:GetDataStore('GuarulhosRP')
--//
local function SetUp(player)
local folder = Instance.new('Folder', player)
folder.Name = 'Data'
local banco = Instance.new('NumberValue', folder)
banco.Name = 'Banco'
local carteira = Instance.new('NumberValue', folder)
carteira.Name = 'Carteira'
local folder2 = Instance.new('Folder', folder)
folder2.Name = 'Prisao'
local preso = Instance.new('NumberValue', folder2)
preso.Name = 'Preso'
local solto = Instance.new('NumberValue', folder2)
solto.Name = 'Solto'
local tempocadeia = Instance.new('NumberValue', folder2)
tempocadeia.Name = 'TempoCadeia'
local notifica = game.Players[player.Name].PlayerGui:WaitForChild('Notify')
local notexto = notifica.Texto
local fail, success = pcall(function()
info = statsstore:GetAsync(tostring(player.UserId))
end)
if success then
local data = game.Players[player.Name].Data
local banco = data.Banco
local carteira = data.Carteira
local prisao = data.Prisao
local preso = prisao.Preso
local solto = prisao.Solto
local tempocadeia = prisao.TempoCadeia
banco.Value = info[1]
carteira.Value = info[2]
preso.Value = info[3]
solto.Value = info[4]
tempocadeia.Value = info[5]
notifica.Enabled = true
notexto.Text = 'Dados carregados com sucesso'
wait(2)
for i = 1,0,-0.1 do
wait()
notexto.TextTransparency = i
end
wait(1)
for i = 0,1,0.1 do
wait()
notexto.TextTransparency = i
end
notifica.Enabled = false
else
print('Nenhum dado encontrado para '..player.Name)
wait(2)
notifica.Enabled = true
notexto.Text = 'Bem-vindo à Guarulhos, '..player.Name..'.'
for i = 1,0,-0.1 do
wait()
notexto.TextTransparency = i
end
wait(2)
for i = 0,1,0.1 do
wait()
notexto.TextTransparency = i
end
notifica.Enabled = false
end
end
local function Disconnect(player)
local data = game.Players[player.Name].Data
local banco = data.Banco
local carteira = data.Carteira
local prisao = data.Prisao
local preso = prisao.Preso
local solto = prisao.Solto
local tempocadeia = prisao.TempoCadeia
local fail, success = pcall(function()
statsstore:SetAsync(tostring(player.UserId), banco.Value, carteira.Value, prisao.Value, preso.Value, solto.Value, tempocadeia.Value)
end)
if success then
print('O jogador '..player.Name..' saiu, porém seus dados salvaram')
else
print('O jogador '..player.Name..' saiu, porém seus dados NÃO salvaram')
end
end
game.Players.PlayerAdded:connect(SetUp)
game.Players.PlayerRemoving:connect(Disconnect)
Could you try putting a print("Save success!") right after the :SetAsync call and autosave every 30 seconds? Rejoin the server after you see the “Save success!” message and see if the issue still persists. It’s unlikely but maybe your data store calls are taking a very long time to execute. Additionally, :SetAsync and :UpdateAsync calls in a studio environment tend to never actually run when the test server is being closed.
So, there’s a print after :SetAsync() and it prints as if the stats were properly saved, I’m not having a problem with saving them but when they load in. The game returns no data, as I told it to print so if no data was found. Also, I don’t have any clue on how will I do an autosave since I don’t know how I’ll reference the player in a Server Script without a function that runs in specific conditions (e.g. Players joins, Player removed)
Well, that quite makes a lot of sense thinking of it now, I applied the change you told me but now it has problems saving other than loading. Guess cavemen code isn’t a good thing to do. No output response other than “No data was saved”.
I believe this may be your issue. SetAsync takes two arguments. Your first one being correct as the key, but the second one can only be ONE value. You may want to store these multiple values into one table such as:
Alright, I’ve done that but it still tells me it failed to save when I left the server. I can’t really tell what’s wrong with my code at this point, everything seems to be working fine.
This is the current code with all the previous suggestions applied. Sorry if I’m taking too long to find an answer to this problem, still quite new to scripting, especially datastores.
--< Variables >--
local datastoreservice = game:GetService('DataStoreService')
local statsstore = datastoreservice:GetDataStore('GuarulhosRP')
--//
local function SetUp(player)
local folder = Instance.new('Folder', player)
folder.Name = 'Data'
local banco = Instance.new('NumberValue', folder)
banco.Name = 'Banco'
local carteira = Instance.new('NumberValue', folder)
carteira.Name = 'Carteira'
local folder2 = Instance.new('Folder', folder)
folder2.Name = 'Prisao'
local preso = Instance.new('NumberValue', folder2)
preso.Name = 'Preso'
local solto = Instance.new('NumberValue', folder2)
solto.Name = 'Solto'
local tempocadeia = Instance.new('NumberValue', folder2)
tempocadeia.Name = 'TempoCadeia'
local notifica = game.Players[player.Name].PlayerGui:WaitForChild('Notify')
local notexto = notifica.Texto
local success, fail = pcall(function()
info = statsstore:GetAsync(tostring(player.UserId))
end)
if success and info ~= nil then
local data = game.Players[player.Name].Data
local banco = data.Banco
local carteira = data.Carteira
local prisao = data.Prisao
local preso = prisao.Preso
local solto = prisao.Solto
local tempocadeia = prisao.TempoCadeia
banco.Value = info[1]
carteira.Value = info[2]
preso.Value = info[3]
solto.Value = info[4]
tempocadeia.Value = info[5]
notifica.Enabled = true
notexto.Text = 'Dados carregados com sucesso'
wait(2)
for i = 1,0,-0.1 do
wait()
notexto.TextTransparency = i
end
wait(1)
for i = 0,1,0.1 do
wait()
notexto.TextTransparency = i
end
notifica.Enabled = false
else
print('Nenhum dado encontrado para '..player.Name)
wait(2)
notifica.Enabled = true
notexto.Text = 'Bem-vindo à Guarulhos, '..player.Name..'.'
for i = 1,0,-0.1 do
wait()
notexto.TextTransparency = i
end
wait(2)
for i = 0,1,0.1 do
wait()
notexto.TextTransparency = i
end
notifica.Enabled = false
end
end
local function Disconnect(player)
local data = game.Players[player.Name].Data
local banco = data.Banco
local carteira = data.Carteira
local prisao = data.Prisao
local preso = prisao.Preso
local solto = prisao.Solto
local tempocadeia = prisao.TempoCadeia
local success, fail = pcall(function()
statsstore:SetAsync(tostring(player.UserId), {banco.Value, carteira.Value, prisao.Value, preso.Value, solto.Value, tempocadeia.Value})
end)
if success then
print('O jogador '..player.Name..' saiu, porém seus dados salvaram')
else
print('O jogador '..player.Name..' saiu, porém seus dados NÃO salvaram')
end
end
game.Players.PlayerAdded:connect(SetUp)
game.Players.PlayerRemoving:connect(Disconnect)
Lmao, you won’t believe it, I was trying to save a Folder with .Value alongside it. It works perfectly now, that was very stupid, can’t believe it’s been over 12 hours trying to find a solution to this. Cavemen coding time.
Haha yeah don’t worry, the easiest solutions are usually the hardest to find sometimes. If I helped at all then please mark me as a solution and good luck with whatever you are working on!