Hey I'm trying to make a leaderboard for deaths and my script doesn't really seem to track deaths and it's not showing any errors. I have two scripts, one for creating the leaderboard and one for tracking deaths. Here's my scripts, if possible could you explain what I did wrong? Thanks! Creating leaderboard script:
game.Players.PlayerAdded:Connect(function(player) local leaderstats = Instance.new("Folder",player) leaderstats.Name = "leaderstats" local Deaths = Instance.new("IntValue",leaderstats) Deaths.Name = "Deaths" Deaths.Value = 0 end)
Tracking deaths script:
local function playerDead(player) local Deaths = player:WaitForChild("leaderstats").Deaths local hum = player.Parent:FindFirstChild("Humanoid") if hum then if hum.Health == 0 then Deaths.Value = Deaths.Value + 1 end end end game.Players.PlayerAdded:Connect(playerDead)
Hello, AtrocityPrevails! There are quite a few problems with your script. First, you typed player.Parent:FindFirstChild("Humanoid")
. This wouldn't work because the player's parent isn't its character. Instead, type player.Character
. Second, the tacking deaths script would only work once because when the character respawns, the character that died would've gotten destroyed. Third, you used an if statement without a while loop. This wouldn't work because you wouldn't be constantly checking for the humanoid to die. Try this script:
game:GetService("Players").PlayerAdded:Connect(function(player) local leaderstats = Instance.new("Folder") leaderstats.Name = "leaderstats" leaderstats.Parent = player local Deaths = Instance.new("IntValue") Deaths.Name = "Deaths" Deaths.Parent = leaderstats player.CharacterAdded:Connect(function(character) character.Humanoid.Died:Connect(function() Deaths.Value = Deaths.Value + 1 end) end) end)
IMPROVEMENTS:
I used :GetService(String Service)
instead of the conditional dot operator. I also made it so that you don't use the second argument of Instance.new(string instance)
since it's inefficient. Instead, manually set it by typing leaderstats.Parent = player, etc.
The script above works because there's an event that fires when a character is added, and inside that event function, there is an event the fires when the player dies. When it does, it adds + 1 to the Deaths value. Hopefully, this helped you. Please accept my answer if it did.
Instead of the current function you are using for player deaths, use this instead:
game.Players.PlayerAdded:Connect(function(Player) Player.CharacterAdded:Connect(function(Character) local Humanoid = Character:FindFirstChild("Humanoid") Humanoid.Died:Connect(function() Player.leaderstats.Deaths.Value = Player.leaderstats.Deaths.Value + 1 end) end) end)