Use :BindToClose instead of PlayerRemoving or else the Data won’t save if a player loses connection or if the game shuts down. PlayerRemoving only works when a player leaves the game intentionally.
@regexman weren’t you the one who told me to use BindToClose instead of PlayerRemoving in the first place?
local Players = game:GetService("Players")
local DataStore = game:GetService("DataStoreSerivce"):GetDataStore("DataStoreValues")
local RunService = game:GetService("RunService")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Parent = player
leaderstats.Name = "leaderstats"
local Strength = Instance.new("IntValue")
Strength.Parent = leaderstats
Strength.Name = "Strength"
Strength.Value = 1
local Exp = Instance.new("NumberValue")
Exp.Parent = leaderstats
Exp.Name = "Exp"
Exp.Value = 0
local RequiredExp = Instance.new("NumberValue")
local
RequiredExp.Name = "RequiredExp"
RequiredExp.Value = Strength.Value * 100
Exp.Changed:Connect(function(Changed)
if Exp.Value >= RequiredExp.Value then
Exp.Value = 0
Strength.Value += 1
RequiredExp.Value = Strength.Value * 100
end
end)
local value1Data = Strength
local value2Data = Exp
local s, e = pcall(function()
value1Data = DataStore:GetAsync(player.UserId.."-Value1") or 0 --check if they have data, if not it'll be "0"
value2Data = DataStore:GetAsync(player.UserId.."Value1") or 0
end)
if s then
Strength.Value = value1Data --setting data if its success
Exp.Value = value2Data
else
game:GetService("TestService"):Error(e) --if not success then we error it to the console
end
end)
game:BindToClose(function(player)
if not RunService:IsStudio()
local s, e = pcall(function()
DataStore:SetAsync(player.UserId.."-Value1", player.leaderstats.Strength.Value) --setting data
DataStore:SetAsync(player.UserId.."-Value2", player.leaderstats.Exp.Value)
end)
if not s then game:GetService("TestService"):Error(e)
end
end)
end)