hello. not too sure why these deaths aren’t counting for the leaderstats if anyone could help me thank you for your time. there isn’t any errors in the output by the looks of things. if u need me to provide u with more info ill gladly be willing to do so. thanks.
leaderstats script
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
2nd part to leaderstats script
local player = game.Players.LocalPlayer or game.Players.PlayerAdded:Wait()
local char = player.Character or player.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid")
humanoid.Died:Connect(function(plr)
plr.leaderstats.Deaths.Value = plr.leaderstats.Deaths.Value + 1
end)
end
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local SaveDataStore = DataStoreService:GetDataStore("SaveData")
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:SetAsync(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 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
while wait() do
local player = game.Players.LocalPlayer or game.Players.PlayerAdded:Wait()
local char = player.Character or player.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid")
humanoid.Died:Connect(function()
player.leaderstats.Deaths.Value = player.leaderstats.Deaths.Value + 1
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)
end)
If the Humanoid.Died() function is inside the game.Players.PlayerAdded function at the top of the script you don’t need to define the player anymore I.e you can get rid of the line I stated.
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local SaveDataStore = DataStoreService:GetDataStore("SaveData")
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:SetAsync(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 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
while wait() do
local char = player.Character or player.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid")
humanoid.Died:Connect(function()
player.leaderstats.Deaths.Value = player.leaderstats.Deaths.Value + 1
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)
end)
When the leaderboard does work, do the deaths count?
As for the fact that it doesn’t work, I’m not really sure why the leaderstats do and don’t appear, you’ve seem to create the leaderstats just fine.