My game is not saving items and currency every single time. I notice that sometimes it saves the currency when leaving, other times it does not, it does not save any items or the leaderstats I buy in my game AT ALL. Why is this? My scripts are fine right?
This script is for when players join a check. It checks their money, level, and if they bought an item (when buying the item, it makes the int value, hp equal to 1)
local DataStore = game:GetService("DataStoreService"):GetDataStore("mymdata")
game.Players.PlayerAdded:connect(function(player)
local leaderboardstats = Instance.new("Model", player)
leaderboardstats.Name = "leaderstats"
local status = Instance.new("IntValue")
status.Parent = leaderboardstats
status.Name = "Money"
print("Getting money value for player")
local levels = Instance.new("IntValue")
levels.Parent = leaderboardstats
levels.Name = "Level"
local healing = Instance.new("IntValue", player)
healing.Name = "hp" -- Healing Potion
local key = "player-"..player.userId
local savedValues = DataStore:GetAsync(key)
if savedValues then
status.Value = savedValues[1]
levels.Value = savedValues[2]
healing.Value = savedValues[3]
else
local valuesToSave = {status.Value, levels.Value, healing.Value}
DataStore:SetAsync(key, valuesToSave)
end
if healing.Value == 1 then
print('healing is true')
local item = game.StorageServer.Potion ---ITEMHERE
item:clone().Parent = player.StarterGear
item:clone().Parent = player.Backpack
else
wait()
end
end)
Now I have this script for when players leave
local DataStore = game:GetService("DataStoreService"):GetDataStore("mydata")
game.Players.PlayerRemoving:connect(function(player)
local key = "player-"..player.userId
local valuesToSave = {player.leaderstats.Coins.Value, player.leaderstats.Level.Value, player.hp.Value}
DataStore:SetAsync(key, valuesToSave)
print("Saved to data store")
end)

1 comment
As this is most likely a server-Script, the latency should not matter (by the time the server knows a player is disconnecting they are already disconnecting). Anyway, I would be suprised if PlayerRemoving is called after the Player object has already been removed (it is RemovING, after all).