I’m currently trying to save a couple pieces of Data for a small project but every time i leave the game the Data of Trophies
and Jump Power
doesn’t save. I don’t know if its because when i do player removing the player leaves before the data can be saved or because something else is wrong.
local Datastore = DataStoreService:GetDataStore("PlayerData")
local Players = game:GetService("Players") -- Just to simplify things a little
Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local Jumps = Instance.new("IntValue", leaderstats)
Jumps.Name = "Jumps"
local Trophies = Instance.new("IntValue", leaderstats)
Trophies.Name = "Trophies"
local Data = {
Jumps = 0,
Rebirths = 0,
Trophies = 0, -- or {}
Inventory = {}
}
local success, err = pcall(function()
Data = Datastore:GetAsync(player.UserId) or Data -- Set it to the default value above if it doesn't exist
end)
if not success then
warn(err)
end
Jumps.Value = Data["Jumps"]
Trophies.Value = Data["Trophies"]
player.CharacterAdded:Connect(function(character)
local Character = player.Character or player.CharacterAdded:Wait()
local Humanoid = Character.Humanoid
Humanoid.UseJumpPower = true
Humanoid.JumpPower = Jumps.Value
end)
Jumps.Changed:Connect(function()
task.wait(3)
player.Character.Humanoid.JumpPower = Jumps.Value
end)
end)
Players.PlayerRemoving:Connect(function(player)
Datastore:SetAsync(player.UserId, {
Jumps = player.Jumps.Value,
Rebirths = 0,
Trophies = player.Trophies.Value, -- or {}
Inventory = {}
})
end)
while true do
for _, player in Players:GetPlayers() do
pcall(function() -- This is just to prevent one player from messing it all up
player.leaderstats.Jumps.Value += 1
player.leaderstats.Trophies.Value += 1
end)
end
task.wait(0.5)
end