Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

How do I track deaths for my leaderboard script?

Asked by 4 years ago
Edited 4 years ago

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)

2 answers

Log in to vote
2
Answered by 4 years ago

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.

0
Ah you are 49 seconds faster. :) oilkas 364 — 4y
0
Thank you so much for the help! Also, can you explain the GetService thing? When would you use that? AtrocityPrevails 49 — 4y
0
Oh, yeah. One of the good things about being a scripter is that you can type fast. youtubemasterWOW 2741 — 4y
1
GetService("Players") is basically the same thing as game.Players. It's a good habit to use GetService() almost all the time since it checks if a service is existent. If it isn't it creates that service. youtubemasterWOW 2741 — 4y
View all comments (3 more)
0
Wow, you're an amazing scripter! Thanks bro AtrocityPrevails 49 — 4y
0
No problem. youtubemasterWOW 2741 — 4y
Ad
Log in to vote
1
Answered by
oilkas 364 Moderation Voter
4 years ago

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)
0
Hey thanks for the help! I learned the 'Died' thing from you today :) AtrocityPrevails 49 — 4y
0
No problem! oilkas 364 — 4y

Answer this question