After i rejoin my game, all my values go 0, please help me.
local ds = game:GetService("DataStoreService") local data = ds:GetDataStore("FruitSimulatorDataStore02") game.Players.PlayerAdded:Connect(function(plr) local stats = Instance.new("Folder", game.ReplicatedStorage) stats.Name = plr.Name.."Stats" local coins = Instance.new("NumberValue",stats) coins.Name = "Coins" coins.Value = data:GetAsync(plr.UserId) or 0 local baglvl = Instance.new("NumberValue",stats) baglvl.Name = "Bag" baglvl.Value = data:GetAsync(plr.UserId) or 15 local fruits = Instance.new("NumberValue", stats) fruits.Name = "Fruits Collected" fruits.Value = data:GetAsync(plr.UserId) or 0 local world2unlocked = Instance.new("NumberValue",stats) world2unlocked.Name = "W2Unlocked" world2unlocked.Value = data:GetAsync(plr.UserId) or 0 end) game.Players.PlayerRemoving:Connect(function(player) data:SetAsync(player.UserId, game.ReplicatedStorage[player.Name.."Stats"].Coins.Value, game.ReplicatedStorage[player.Name.."Stats"]["Fruits Collected"].Value, game.ReplicatedStorage[player.Name.."Stats"].Bag.Value, game.ReplicatedStorage[player.Name.."Stats"].W2Unlocked.Value) print("Saved "..player.Name.."'s Stats.") end)
Your script looks fine; however, I would advise you to save your values in a table as there is a limited request count with saving multiple values in one data store. Now, I will address what I think is your problem. Try adding the :BindToClose
function to your script because there are times when the server shuts down before the last player's data is saved. This could also happen while you're playing solo in the Studio. :BindToClose
will allow all your remaining functions (including PlayerRemoving) to run less than 30 seconds before the server shuts down. Simply add this script to yours at the end:
game:BindToClose(function() for i, player in pairs(game.Players:GetPlayers()) do -- loops through all the players in the server local values = { game.ReplicatedStorage[player.Name.."Stats"].Coins.Value, game.ReplicatedStorage[player.Name.."Stats"]["Fruits Collected"].Value, game.ReplicatedStorage[player.Name.."Stats"].Bag.Value, game.ReplicatedStorage[player.Name.."Stats"].W2Unlocked.Value } data:SetAsync(player.UserId, values) print("Saved "..player.Name.."'s Stats.") end end)
So I Have Made A Model And I Gave This To Someone Before, Just Change The Values To Yours And It Should Save!
If This Helps or Theres A Problem Tell Me
Link: https://www.roblox.com/library/5061331155/DataStore-SAVING-LEADERSTATS