I've tested this script in studios and it does the same thing in the actual game, It saves data and 50% of the time it doesn't. I'm relatively new to DataStore so I thought it was a good idea to ask you guys.
local DataStore = game:GetService("DataStoreService") local ds = DataStore:GetDataStore("LeaderboardSaveSystem") switch = false switch2 = false game.Players.PlayerAdded:connect(function(player) local leader = Instance.new("Folder",player) leader.Name = "leaderstats" local Speed = Instance.new("NumberValue",leader) local Rebirth = Instance.new("NumberValue",leader) Speed.Name = "Speed" Rebirth.Name = "Rebirth" Speed.Value = ds:GetAsync(player.UserId) Speed.Value = ds:GetAsync(player.UserId) ds:SetAsync(player.UserId, Speed.Value) ds:SetAsync(player.UserId, Rebirth.Value) Speed.Changed:connect(function() if switch == false then switch = true print("sanic") ds:SetAsync(player.UserId, Speed.Value) wait(7) switch = false end end) Rebirth.Changed:connect(function() if switch2 == false then switch2 = true print("rebirth'd") ds:SetAsync(player.UserId, Rebirth.Value) wait(7) switch2 = false end end) end) game.Players.PlayerRemoving:connect(function(player) print("player left") ds:SetAsync(player.UserId, player.leaderstats.Speed.Value) ds:SetAsync(player.UserId, player.leaderstats.Rebirth.Value) end)
As you are relatively new to DataStore, one fatal flaw is in your code. When you save to a DataStore, you need to give a key and a value to save to that key.
In this example:
ds:SetAsync(player.UserId, Speed.Value)
you are saving the Speed.Value
to the key player.UserId
However, you are overwriting that same key here:
ds:SetAsync(player.UserId, Rebirth.Value)
This causes only one thing to save.
An easy solution to fix this would be to just give custom names to the keys, such as:
ds:SetAsync(player.UserId.."rebirth", Rebirth.Value) ds:SetAsync(player.UserId.."speed", Speed.Value)
This makes the keys specific for each player, and themselves. This should work perfectly as long as you put it anywhere you save/load the data.
Hopefully this helps, and if you have any questions please ask!