So i have made this script thats suposed to save the players Cash and the vehicles it has purchased, tho for some reason the vehicles purchased dont get saved only the Cash
value.
Script:
local DataStore = game:GetService("DataStoreService") local ds1 = DataStore:GetDataStore("RTPD")
game.Players.PlayerAdded:connect(function(player) local leader = Instance.new("Folder", player) leader.Name = "leaderstats"
local Cash = Instance.new("IntValue", leader) Cash.Name = "Cash" Cash.Value = ds1:GetAsync(player.UserId) or 0 ds1:SetAsync(player.UserId, Cash.Value) game.StarterGui.PurchaseGUI.Frame.Zilko_Natalia.Purchased.Value = ds1:GetAsync(player.UserId) or false game.StarterGui.PurchaseGUI.Frame.Voyage_D.Purchased.Value = ds1:GetAsync(player.UserId) or false game.StarterGui.PurchaseGUI.Frame.DA_Ferra.Purchased.Value = ds1:GetAsync(player.UserId) or false game.StarterGui.PurchaseGUI.Frame.DA_Venerra.Purchased.Value = ds1:GetAsync(player.UserId) or false game.StarterGui.PurchaseGUI.Frame.Hektor_Mediona.Purchased.Value = ds1:GetAsync(player.UserId) or false game.StarterGui.PurchaseGUI.Frame.Mounts_Rake.Purchased.Value = ds1:GetAsync(player.UserId) or false game.StarterGui.PurchaseGUI.Frame.Novena_Acesera.Purchased.Value = ds1:GetAsync(player.UserId) or false game.StarterGui.PurchaseGUI.Frame.Roland_Motors_GTR.Purchased.Value = ds1:GetAsync(player.UserId) or false game.StarterGui.PurchaseGUI.Frame.Surge_Maple_R.Purchased.Value = ds1:GetAsync(player.UserId) or false game.StarterGui.PurchaseGUI.Frame.Voyage_Jalanster_Betamobili.Purchased.Value = ds1:GetAsync(player.UserId) or false game.StarterGui.PurchaseGUI.Frame.Voyage_Pengangkuter_30_CWT.Purchased.Value = ds1:GetAsync(player.UserId) or false wait(5) --Cash.Changed:connect(function() print("SavingData") ds1:SetAsync(game.StarterGui.PurchaseGUI.Frame.Zilko_Natalia.Purchased.Value) ds1:SetAsync(game.StarterGui.PurchaseGUI.Frame.Voyage_D.Purchased.Value) ds1:SetAsync(game.StarterGui.PurchaseGUI.Frame.DA_Ferra.Purchased.Value) ds1:SetAsync(game.StarterGui.PurchaseGUI.Frame.DA_Venerra.Purchased.Value) ds1:SetAsync(game.StarterGui.PurchaseGUI.Frame.Hektor_Mediona.Purchased.Value) ds1:SetAsync(game.StarterGui.PurchaseGUI.Frame.Mounts_Rake.Purchased.Value) ds1:SetAsync(game.StarterGui.PurchaseGUI.Frame.Novena_Acesera.Purchased.Value) ds1:SetAsync(game.StarterGui.PurchaseGUI.Frame.Roland_Motors_GTR.Purchased.Value) ds1:SetAsync(game.StarterGui.PurchaseGUI.Frame.Surge_Maple_R.Purchased.Value) ds1:SetAsync(game.StarterGui.PurchaseGUI.Frame.Voyage_Jalanster_Betamobili.Purchased.Value) ds1:SetAsync(game.StarterGui.PurchaseGUI.Frame.Voyage_Pengangkuter_30_CWT.Purchased.Value) ds1:SetAsync(player.UserId, Cash.Value) end)
--end)
game.Players.PlayerRemoving:connect(function(player) ds1:setAsync(player.UserId, player.leaderstats.Cash.Value)
ds1:SetAsync(game.StarterGui.PurchaseGUI.Frame.Zilko_Natalia.Purchased.Value) ds1:SetAsync(game.StarterGui.PurchaseGUI.Frame.Voyage_D.Purchased.Value) ds1:SetAsync(game.StarterGui.PurchaseGUI.Frame.DA_Ferra.Purchased.Value) ds1:SetAsync(game.StarterGui.PurchaseGUI.Frame.DA_Venerra.Purchased.Value) ds1:SetAsync(game.StarterGui.PurchaseGUI.Frame.Hektor_Mediona.Purchased.Value) ds1:SetAsync(game.StarterGui.PurchaseGUI.Frame.Mounts_Rake.Purchased.Value) ds1:SetAsync(game.StarterGui.PurchaseGUI.Frame.Novena_Acesera.Purchased.Value) ds1:SetAsync(game.StarterGui.PurchaseGUI.Frame.Roland_Motors_GTR.Purchased.Value) ds1:SetAsync(game.StarterGui.PurchaseGUI.Frame.Surge_Maple_R.Purchased.Value) ds1:SetAsync(game.StarterGui.PurchaseGUI.Frame.Voyage_Jalanster_Betamobili.Purchased.Value) ds1:SetAsync(game.StarterGui.PurchaseGUI.Frame.Voyage_Pengangkuter_30_CWT.Purchased.Value)
end)
in the PlayerRemoving part you need to add
--at the top local SavedData = instance.new("BindableEvent") --At the end SavedData:Fire() game:BindToClose(function() SavedData.Event:Wait() end)
and you forget to put a key. instead of player.UserId do player.UserId.."Cash" for cash and player.UserdId.."Purchased_[carname]" eg
DS1:SetAsync(**The long line**, player.UserdId.."Purchased_**the name of the car**"
Because if you are the last player if you leave the server will shut down so DataStore has no time to save it. What game:BindToClose
does, if the server shuts down it will call the function until the function ends, the function ends when the data saved succesfully