The title says it all, everything is in one script which is the leaderstats script. There are no errors relating to the deaths in the script. Thanks for your time and I will provide you with more information if needed.
leaderstats script:
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
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
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)
@RudraVIIâs solution should fix your problem. However, note that you have to place that player.CharacterAddedbefore the while loop, or your function will never be connected.
Also, just a small observation, in your script you are parenting an instance to the same object twice for no reason:
local Deaths = Instance.new("IntValue", leaderstats) -- create an IntValue and parent it to leaderstats
...
Deaths.Parent = leaderstats -- parent the created IntValue to leaderstats
As you can see, this is redundant (just delete Instance.new 2nd argument or that last command).
Your functionâs argument is player (no capital letter), but if that was the problem it should be marked with a blue line. Red lines inform you that the script will not run properly, e.g. because of a syntax error.
In these cases, you can right click > press âGo to script errorâ and read whatâs exactly causing the problem. Maybe it is just that you didnât complete the statement yet.
Nope that was just a segment. Here is the full script:
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
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()
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)