I have been trying to do to Instances/leaderstats/Datastores and I have tried multiple things, like making more variables, datastores, etc
local DataStore = game:GetService("DataStoreService") local ds1 = DataStore:GetDataStore("YouCantSeeMyDataStore") local ds2 = DataStore:GetDataStore("NopeBOIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII") game.Players.PlayerAdded:connect(function(player) local leader = Instance.new("Folder",player) leader.Name = "leaderstats" local Pointz = Instance.new("IntValue",leader) Pointz.Name = "Pointz" Pointz.Value = ds1:GetAsync(player.UserId) or 0 ds1:SetAsync(player.UserId,Pointz.Value) Pointz.Changed:connect(function() print("Saving Data") ds1:SetAsync(player.UserId,Pointz.Value) print(player.UserId.."s Data of"..Pointz.Value.." "..Pointz.Name.." Has Been Saved!") end) local Wins = Instance.new("IntValue",leader) Wins.Name = "Wins" Wins.Value = ds2:GetAsync(player.UserId) or 0 ds2:SetAsync(player.UserId,Wins.Value) Wins.Changed:connect(function() ds2:SetAsync(player.UserId,Wisn.Value) end) end) game.Players.PlayerRemoving:connect(function(player) print("Saving Data") ds1:SetAsync(player.UserId,player.leaderstats.Pointz.Value) ds2:SetAsync(player.UserId,player.leaderstats.Wins.Value) print(player.UserId.."s Data of"..player.leaderstats.Pointz.Value..""..player.leaderstats.Pointz.Name.."Has Been Saved!") end) --
That is what I tried
When asking questions, you should be more specific about the problem. So far I see only one/two problems with the code. Line 22 should be:
ds2:SetAsync(player.UserId,Wins.Value)
Although this is mostly speculation, I have found from personal experience that saving multiple values in one function connected to PlayerRemoving
causes issues in Play Solo (not sure about online), so I would do the following instead:
game.Players.PlayerRemoving:connect(function(player) ds1:SetAsync(player.UserId,player.leaderstats.Pointz.Value) end game.Players.PlayerRemoving:connect(function(player) ds2:SetAsync(player.UserId,player.leaderstats.Wins.Value) end
Again, I'm not sure if this is an issue, but it's worth trying.
Hey,
I have re-written it in my own way..Your code was just too messy for me. First of all, you don't need two GetDataStore()'s as you can store multiple data in the same key. I also removed player removing, as its kind of useless since you're updating it when the value changes. You should never only use PlayerRemoving only.(That was the case for Data Persistence), but I remain with that thought with DataStores.
It should also be noted that there is a request limit with DataStores.
I explained everything with the help of commenting...very fast so don't expect the quality and explanations to be fantastic.
I heavily suggest you read the wiki page on Datastore here
Cheers!
local DataStore = game:GetService("DataStoreService"):GetDataStore("Datastoregot"); -- Getting the DataStore service, giving the database a name function savestats(plr) --This function will run everytime the .Changed event is called either on the points or the wins print("Savestats went"); local key = plr.userId; --This can be whatever you like, make sure its unique with each user. Best way would be to have it as the player ID and not to use their username local valuestoSave = {plr.leaderstats.Points.Value, plr.leaderstats.Wins.Value};--These are the two values we want to save into that unique key. DataStore:SetAsync(key,valuestoSave);--Saving the data into that unique key with SetAsync. You can use UpdateAsync, and read about it on the wiki if you want. print("Saved"); end game.Players.PlayerAdded:connect(function(plr) --Everytime a player is added to the game, this event will fire. local leaderstats = Instance.new("Model",plr); --Creating leaderstats Model. Notice that I parented it by using a comma. leaderstats.Name = "leaderstats"; --Has to be named leaderstats local points = Instance.new("IntValue",leaderstats) --Creating points points.Name = "Points"; --Naming points points.Changed:connect(function()--Fires every time the value of points changes savestats(plr;)-- This is where we fire that function I talked about above end) local wins = Instance.new("IntValue", leaderstats;)--Creating wins wins.Name = "Wins"; --Naming wins wins.Changed:connect(function()--Fires every time the value of wins changes savestats(plr); --This is where we fire that function I talked about above end) local key = plr.userId; --The unique key, I had before. Always make sure the key is the same local savedstats = DataStore:GetAsync(key);--Get all the data that's stored in that unique key if savedstats then --If we found data then, points.Value = savedstats[1];--Change the value from 0 to the saved value wins.Value = savedstats[2];--Change the value from 0 to the saved value else--If you don't local valuestosave = {points.Value, wins.Value};--All the values we need to save DataStore:SetAsync(key,valuestosave);--Put the new user in the database end end)