so im trying to make the datastore save but it does not print Player data was saved it also does not save the data. it does not give a error and i have studio api services enabled code:
local DS = game:GetService("DataStoreService") local SaveMoney = DS:GetDataStore("SaveMoney") local SaveDiamond = DS:GetDataStore("SaveDiamond") local function PlayerAdded(Plr) local Stats = Instance.new("Folder", Plr) Stats.Name = "stats" local Money = Instance.new("IntValue", Stats) Money.Name = "Money" Money.Value = SaveMoney:GetAsync(Plr.UserId) or 500 local Mone2y = Instance.new("IntValue", Stats) Mone2y.Name = "diam" Mone2y.Value = SaveDiamond:GetAsync(Plr.UserId) or 0 while true do wait(300) local MyGameStuff = script.Moneyz:Clone() local ValueLol = math.random(10,285) MyGameStuff.Frame.amount.Text = ValueLol MyGameStuff.Frame.amount.CashToGive.Value = ValueLol MyGameStuff.Parent = Plr.PlayerGui end end local function PlayerRemoving(Plr) local success, errormessage = pcall(function() SaveMoney:SetAsync(Plr.stats.Money.Value, Plr.UserId) SaveDiamond:SetAsync(Plr.stats.diam.Value, Plr.UserId) end) if success then print("Player data was saved") else warn(errormessage) end end game.Players.PlayerAdded:Connect(PlayerAdded) game.Players.PlayerRemoving:Connect(PlayerRemoving)
When the last player in the server leaves PlayerRemoving
doesnt fire. To save the data of the last player in the server you should use game:BindToClose()
This will run the function before the server shuts down and save the data of the last player.
Keep in mind; BindToClose()
will only keep the server from shutting down until the function finishes running or until 30 seconds have passed from the moment it started so make sure your function doesn't take that long to execute or you will have dataloss.
Well it’s most likely because you’re either trying it out on studio and not on a real server or because the server hasn’t got enough time to save before it shuts down. I’d recommend using BindToClose so it will save.
Adding on to what OwnYou said, you can create a seperate thread for each player ingame at the time so that it effectively saves all of their data.
local function Save(Key, Data) pcall(function() DataStore:SetAsync(Key, Data); end) end game:BindToClose(function() for _,v in pairs(game.Players:GetPlayers()) do coroutine.wrap(Save)(v.UserId, true); end end)