I am making a datastore for a game that saves the values “Rebirths” and “Taps” from a leaderstats folder and loads it in when the player joined. I got the datastore to work for one value but I don’t know how to get it to work for “Rebirths” too. I tried modifying the script to this but it just prints an error “ServerScriptService.WorldData:49: attempt to index number with number” and sets both values to 0 even when 1 already had a value stored. Can someone tell me what i’m doing wrong?
local DSS = game:GetService("DataStoreService")
local playerStats = DSS:GetDataStore("playerStats")
game.Players.PlayerAdded:Connect(function(plr)
local taps = plr:WaitForChild("leaderstats").Taps
local rebirths = plr:WaitForChild("leaderstats").Rebirths
local data
local succ, err = pcall(function()
data = playerStats:GetAsync(plr.UserId.."_taps", {plr.leaderstats.Taps.Value, plr.leaderstats.Rebirths.Value})
end)
if succ then
taps.Value = data[1]
rebirths.Value = data[2]
print("successfully got data")
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local data = {plr.leaderstats.Taps.Value, plr.leaderstats.Rebirths.Value}
local succ, err = pcall(function()
playerStats:SetAsync(plr.UserId.."_taps", {plr.leaderstats.Taps.Value, plr.leaderstats.Rebirths.Value})
end)
if succ then
print("Success!")
else
print("err")
warn(err)
end
end)
local DSS = game:GetService("DataStoreService")
local playerStats = DSS:GetDataStore("playerStats")
game.Players.PlayerAdded:Connect(function(plr)
local taps = plr:WaitForChild("leaderstats").Taps
local data
local succ, err = pcall(function()
data = playerStats:GetAsync(plr.UserId.."_taps", plr.leaderstats.Taps.Value)
end)
if succ then
taps.Value = data
print("successfully got data")
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local data = plr.leaderstats.Taps.Value
local succ, err = pcall(function()
playerStats:SetAsync(plr.UserId.."_taps", plr.leaderstats.Taps.Value)
end)
if succ then
print("Success!")
else
print("err")
warn(err)
end
end)
You just need to clear old data. You’re getting the old data (a number) and trying to get the first element from it. Changing the key from theiruserid_taps to something else will wipe it, making it work.
Are you testing this in studio? If so (at least in my experience) it won’t save with a player removing event, in fact, you should never use only player removing events to save data. The reason for this is, when there is only one person in the server and they leave, the server shuts down and sometimes, the player removing event doesn’t work. Instead, use a game:BindToClose() event. I don’t really see any errors in the script which is why this is probably the reason. If you don’t want to use BindToClose, there are many, many ways to save data as well. You could add a “Save Data” button, an autosave feature, and more. Relying on only player removing to save data is very inefficient and won’t work in studio (and probably the real game) anyways. Hope this helps!
Player removing events do work in studio. I just playtested in studio and changed my money value to 1000. Closed and began another playtest, my money is now 1000.
As for studio, it never worked for me, maybe I was trying to save too many things and maybe as I said before, the server shut down before it could save everything. Either way, player removing is not reliable if it is the only form of save. You have to add either a bind to close, an autosave, etc. Otherwise, the probability of a player losing data goes up exponentially.
Thank you for testing though, I was corrected on an mistake that I had been doing for a while, which was thinking that studio can’t save using player removing.
I’ll keep this in mind, but I don’t think that it’s the problem here. It only stopped working when I added the few lines of code to try and store rebirths.
This actually might be the problem. Maybe it wasn’t able to save the rebirths before the server shutdown. Could you try making an auto save or something just for testing? If it still doesn’t work, let me know.
Ok, I finally got it to work. I just had to change the name of the datastore the data was in so it would reset. Everything is saving correctly now. Thanks to everyone!