I’m having an issue where peoples XP won’t save after you rejoin. Since this was added in a newer update, it saves peoples XP from the last time they played since the update. I added tools to the recent update (Lift, Lift2, Lift3) and ever since that update it hasn’t been saving. If anyone could possible help me fix this issue with it saving. If you need more information about the issue I’m here to provide it for you.
leaderstats script:
local Players = game:GetService("Players")
local serverStorage = game:GetService("ServerStorage")
local DataStoreService = game:GetService("DataStoreService")
local SaveDataStore = DataStoreService:GetDataStore("SaveData")
local replicatedStorage = game:GetService("ReplicatedStorage")
local remoteData = game:GetService("ServerStorage"):WaitForChild("RemoteData")
local cooldown = 1
local function SavePlayerData(player)
local success, errormsg = pcall(function()
local SaveData = {}
for i, stats in pairs(player.leaderstats:GetChildren()) do
SaveData[stats.Name] = stats.Value
end
SaveDataStore:UpdateAsync(player.UserId, SaveData)
end)
if not success then
return errormsg
end
end
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local level = Instance.new("IntValue")
level.Name = "Level"
level.Value = 0
level.Parent = leaderstats
local XP = Instance.new("IntValue")
XP.Name = "XP"
XP.Value = 0
XP.Parent = leaderstats
local Deaths = Instance.new("IntValue", leaderstats)
Deaths.Name = "Deaths"
Deaths.Value = 0
Deaths.Parent = leaderstats
local dataFolder = Instance.new("Folder")
dataFolder.Name = player.Name
dataFolder.Parent = serverStorage.RemoteData
local debounce = Instance.new("BoolValue")
debounce.Name = "Debounce"
debounce.Parent = dataFolder
local Data = SaveDataStore:GetAsync(player.UserId)
if Data then
for i, stats in pairs(leaderstats:GetChildren()) do
stats.Value = Data[stats.Name]
end
else
print(player.Name .. " has no data.")
end
local expToLevelUp
local expForPreviousLevel = 0
player.CharacterAdded:Connect(function(char)
char.Humanoid.Died:Connect(function()
Deaths.Value = Deaths.Value + 1
end)
end)
replicatedStorage.Remotes.Lift.OnServerEvent:Connect(function(player)
if not remoteData:FindFirstChild(player.Name) then return "NoFolder" end
local debounce = remoteData[player.Name].Debounce
if not debounce.Value then
debounce.Value = true
player.leaderstats.XP.Value = player.leaderstats.XP.Value + 170
wait(cooldown)
debounce.Value = false
end
end)
replicatedStorage.Remotes.Lift.OnServerEvent:Connect(function(player)
if not remoteData:FindFirstChild(player.Name) then return "NoFolder" end
local debounce = remoteData[player.Name].Debounce
if not debounce.Value then
debounce.Value = true
player.leaderstats.XP.Value = player.leaderstats.XP.Value + 170
wait(cooldown)
debounce.Value = false
end
end)
local cooldown = 1
replicatedStorage.Remotes.Lift2.OnServerEvent:Connect(function(player)
if not remoteData:FindFirstChild(player.Name) then return "NoFolder" end
local debounce = remoteData[player.Name].Debounce
if not debounce.Value then
debounce.Value = true
player.leaderstats.XP.Value = player.leaderstats.XP.Value + 80
wait(cooldown)
debounce.Value = false
end
end)
local cooldown = 1
replicatedStorage.Remotes.Lift3.OnServerEvent:Connect(function(player)
if not remoteData:FindFirstChild(player.Name) then return "NoFolder" end
local debounce = remoteData[player.Name].Debounce
if not debounce.Value then
debounce.Value = true
player.leaderstats.XP.Value = player.leaderstats.XP.Value + 320
wait(cooldown)
debounce.Value = false
end
end)
local cooldown = 1
replicatedStorage.Remotes.Lift4.OnServerEvent:Connect(function(player)
if not remoteData:FindFirstChild(player.Name) then return "NoFolder" end
local debounce = remoteData[player.Name].Debounce
if not debounce.Value then
debounce.Value = true
player.leaderstats.XP.Value = player.leaderstats.XP.Value + 230
wait(cooldown)
debounce.Value = false
end
end)
local cooldown = 1
replicatedStorage.Remotes.Lift5.OnServerEvent:Connect(function(player)
if not remoteData:FindFirstChild(player.Name) then return "NoFolder" end
local debounce = remoteData[player.Name].Debounce
if not debounce.Value then
debounce.Value = true
player.leaderstats.XP.Value = player.leaderstats.XP.Value + 50
wait(cooldown)
debounce.Value = false
end
end)
local cooldown = 1
replicatedStorage.Remotes.Lift6.OnServerEvent:Connect(function(player)
if not remoteData:FindFirstChild(player.Name) then return "NoFolder" end
local debounce = remoteData[player.Name].Debounce
if not debounce.Value then
debounce.Value = true
player.leaderstats.XP.Value = player.leaderstats.XP.Value + 160
wait(cooldown)
debounce.Value = false
end
end)
local cooldown = 1
replicatedStorage.Remotes.Lift7.OnServerEvent:Connect(function(player)
if not remoteData:FindFirstChild(player.Name) then return "NoFolder" end
local debounce = remoteData[player.Name].Debounce
if not debounce.Value then
debounce.Value = true
player.leaderstats.XP.Value = player.leaderstats.XP.Value + 120
wait(cooldown)
debounce.Value = false
end
end)
local cooldown = 1
replicatedStorage.Remotes.Lift8.OnServerEvent:Connect(function(player)
if not remoteData:FindFirstChild(player.Name) then return "NoFolder" end
local debounce = remoteData[player.Name].Debounce
if not debounce.Value then
debounce.Value = true
player.leaderstats.XP.Value = player.leaderstats.XP.Value + 130
wait(cooldown)
debounce.Value = false
end
end)
local cooldown = 1
replicatedStorage.Remotes.Lift9.OnServerEvent:Connect(function(player)
if not remoteData:FindFirstChild(player.Name) then return "NoFolder" end
local debounce = remoteData[player.Name].Debounce
if not debounce.Value then
debounce.Value = true
player.leaderstats.XP.Value = player.leaderstats.XP.Value + 90
wait(cooldown)
debounce.Value = false
end
end)
local cooldown = 1
replicatedStorage.Remotes.Lift10.OnServerEvent:Connect(function(player)
if not remoteData:FindFirstChild(player.Name) then return "NoFolder" end
local debounce = remoteData[player.Name].Debounce
if not debounce.Value then
debounce.Value = true
player.leaderstats.XP.Value = player.leaderstats.XP.Value + 50
wait(cooldown)
debounce.Value = false
end
end)
local cooldown = 1
replicatedStorage.Remotes.Lift11.OnServerEvent:Connect(function(player)
if not remoteData:FindFirstChild(player.Name) then return "NoFolder" end
local debounce = remoteData[player.Name].Debounce
if not debounce.Value then
debounce.Value = true
player.leaderstats.XP.Value = player.leaderstats.XP.Value + 240
wait(cooldown)
debounce.Value = false
end
end)
local cooldown = 1
replicatedStorage.Remotes.Lift12.OnServerEvent:Connect(function(player)
if not remoteData:FindFirstChild(player.Name) then return "NoFolder" end
local debounce = remoteData[player.Name].Debounce
if not debounce.Value then
debounce.Value = true
player.leaderstats.XP.Value = player.leaderstats.XP.Value + 60
wait(cooldown)
debounce.Value = false
end
end)
while wait() do
local levelBar = player.PlayerGui:WaitForChild("LevelBar")
if level.Value < 1 then
expToLevelUp = 100
else
expToLevelUp = math.floor(level.Value ^ 1.3) * 200 + math.floor(level.Value ^ 4)
end
if XP.Value >= expToLevelUp then
level.Value = level.Value + 1
end
expForPreviousLevel = math.floor((level.Value - 1) ^ 1.3) * 200 + math.floor((level.Value - 1) ^ 4)
local expDifference = expToLevelUp - expForPreviousLevel
local expDifference2 = XP.Value - expForPreviousLevel
levelBar.Bar:TweenSize(UDim2.new(levelBar.BarBackground.Size.X.Scale * (expDifference2 / expDifference), 0, levelBar.BarBackground.Size.Y.Scale, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Quint, 0.001)
levelBar.Experience.Text = expDifference2 .. "/" .. expDifference
levelBar.Level.Text = "Level: " .. level.Value
XP.Value = XP.Value + 1
end
end)
Players.PlayerRemoving:Connect(function(player)
local errormsg = SavePlayerData(player)
if errormsg then
warn(errormsg)
end
end)
game:BindToClose(function()
for i, player in pairs(Players:GetPlayers()) do
local errormsg = SavePlayerData(player)
if errormsg then
warn(errormsg)
end
end
wait(2)
end)