game.Players.PlayerAdded:connect(function(player) local datastore = game:GetService("DataStoreService"):GetDataStore(player.Name.."Stats") player:WaitForChild("Data") wait(1) local stats = player:FindFirstChild("Data"):GetChildren() for i = 17, #stats do stats[i].Value = datastore:GetAsync(stats[i].Name) print("stat numba "..i.." has been found") end end) -------------------THIS IS ALL GOOD ^^^ game.Players.PlayerRemoving:connect(function(player) local datastore = game:GetService("DataStoreService"):GetDataStore(player.Name.."Stats") local statstorage = player:FindFirstChild("Data"):GetChildren() for i = 17, #statstorage do datastore:SetAsync(statstorage[i].Name, statstorage[i].Value) print("saved data number "..i) end print("Stats successfully saved") end)
I need this script to save all of my data when leaving.
This is the data if it's needed for helping:
game.Players.PlayerAdded:Connect(function(Plr) local stats = Instance.new("Folder", Plr) stats.Name = "Data" --- Level System local Levels = Instance.new("IntValue", stats) Levels.Name = "Levels" Levels.Value = 1 local Exp = Instance.new("IntValue", stats) Exp.Name = "Exp" Exp.Value = 0 local ExpNeed = Instance.new("IntValue", stats) ExpNeed.Name = "ExpNeed" ExpNeed.Value = 200 --- Money System local Beli = Instance.new("IntValue", stats) Beli.Name = "Beli" Beli.Value = 0 local Bounty = Instance.new("IntValue", stats) Bounty.Name = "Bounty" Bounty.Value = 1 --- Stats Text local HealthP = Instance.new("IntValue", stats) HealthP.Name = "HealthP" HealthP.Value = 0 local MeleeP = Instance.new("IntValue", stats) MeleeP.Name = "MeleeP" MeleeP.Value = 0 local SpeedP = Instance.new("IntValue", stats) SpeedP.Name = "SwordP" SpeedP.Value = 0 local DevilFruitP = Instance.new("IntValue", stats) DevilFruitP.Name = "DevilFruitP" DevilFruitP.Value = 0 --- Stats System local Points = Instance.new("IntValue", stats) Points.Name = "Points" Points.Value = 0 local MaxHealth = Instance.new("IntValue", stats) MaxHealth.Name = "MaxHealth" MaxHealth.Value = 0 local Melee = Instance.new("IntValue", stats) Melee.Name = "Melee" Melee.Value = 0 local Speed = Instance.new("NumberValue", stats) Speed.Name = "Sword" Speed.Value = 0 local DevilFruit = Instance.new("IntValue", stats) DevilFruit.Name = "DevilFruit" DevilFruit.Value = 0 --- Sword local Sword = Instance.new("StringValue", stats) Sword.Name = "SwordOwned" Sword.Value = "None" --- Melee local Melee = Instance.new("StringValue", stats) Melee.Name = "MeleeOwned" Melee.Value = "Combat" --- CurrentBoat local CurrentBoat = Instance.new("StringValue", stats) CurrentBoat.Name = "CurrentBoat" CurrentBoat.Value = "None" end) game.Players.PlayerAdded:Connect(function(Plr) wait(.1) local Exp = Plr.Data.Exp local Levels = Plr.Data.Levels local ExpNeed = Plr.Data.ExpNeed local Points = Plr.Data.Points while wait() do if Exp.Value >= (100 * (Levels.Value + 1)) and Levels.Value <= 399 then Levels.Value = Levels.Value + 1 Points.Value = Points.Value + 3 Exp.Value = Exp.Value - ExpNeed.Value ExpNeed.Value = ExpNeed.Value + 100 game.ReplicatedStorage.LevelSystem.LevelUpGui:FireClient(Plr) end end end)
I think you'll need to enable API servers in your game setting. If that doesn't fix it then I don't know what will because the script is fine