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

Why is this not awarding the badge when the player dies?

Asked by
ElBamino 153
3 years ago
Edited 3 years ago

I'm working on something very small. I'm trying to award the badge when the player dies, just dies anyway. This will go to the player who died like a GTA wasted style almost. I was wondering what the problem was with this script I wrote? Thanks for the help, I appreciate it a lot. I'm just here to learn!

local BadgeID = --insert
function WastedBadge(player, BadgeID)
    player.CharacterAdded:Connect(function(character)
        character.Humanoid.Died:Connect(function()
            game:GetService("BadgeService"):AwardBadge(player.Value.UserId.BadgeID)
        end)
    end)
end
game.Players.PlayersAdded:Connect(function(player)
    function WastedBadge(player, BadgeID)
        player.CharacterAdded:Connect(function(character)
            character.Humanoid.Died:Connect(function()
                game:GetService("BadgeService"):AwardBadge(player.Value.UserId.BadgeID)
            end)
        end)
    end
end)

1 answer

Log in to vote
2
Answered by
Mineloxer 187
3 years ago

Multiple things, first looks like you have a typo, as it should be "PlayerAdded" not "PlayersAdded".

Second thing, the function inside the event listener (for PlayerAdded) has another function inside of it called "WastedBadge". You're defining a function, but you never called it so it never ran. Now you can call it, but for simplicity, I would remove it for the code to look like so:

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        character.Humanoid.Died:Connect(function()
            game:GetService("BadgeService"):AwardBadge(player.Value.UserId.BadgeID)
        end)
    end)
end)

Yes, remove the arguments as well, the "player" and "BadgeID".

Third, you seem to have the function duplicated above (WastedBadge), again, you can call it directly, but for now, you can remove that.

Fourth, sometimes scripts load before all of the game objects do, so it's a good habit to keep that in mind and utilize :WaitForChild() to wait for the humanoid. So right before attaching an event listener to the humanoid, wait for it to load:

local humanoid = character:WaitForChild("Humanoid")
humanoid.Died:Connect(function()
    -- rest of code
end)

Finally, :AwardBadge() takes two arguments, the UserId of the player, and the badge Id. To pass multiple arguments to functions, you separate them with commas ","

In addition, UserId is a property of player, so you can directly do player.UserId instead of player.Value.UserId:

game:GetService("BadgeService"):AwardBadge(player.UserId, BadgeID)

These should be all the things you need to get it working. I would suggest trying changes on your own, and then compare it to the final result here:

local BadgeID = 0 -- Badge Id here

-- Listen to a player joining
game.Players.PlayerAdded:Connect(function(player)

    -- Listen to when their character is added
    player.CharacterAdded:Connect(function(character)

        -- Wait for the humanoid to load
        local humanoid = character:WaitForChild("Humanoid")

        -- When the humanoid dies
        humanoid.Died:Connect(function()

            -- Award the badge to the player using their UserId, and the badge Id
            game:GetService("BadgeService"):AwardBadge(player.UserId, BadgeID)

        end)
    end)
end)

Here are some helpful resources:

API reference for :WaitForChild()

https://developer.roblox.com/en-us/api-reference/function/Instance/WaitForChild

API reference for :AwardBadge() and BadgeService

https://developer.roblox.com/en-us/api-reference/function/BadgeService/AwardBadge

https://developer.roblox.com/en-us/api-reference/class/BadgeService

API reference for functions

https://developer.roblox.com/en-us/articles/Function

P.S. It's useful to have the output window open so you can check for any errors that your scripts produce. On the tools bar above in Studio, navigate to View > Output.

1
Thank you very much, I appreciate the help. I really appreciate how you broke it down for me and explained each problem. Thanks again! ElBamino 153 — 3y
0
No problem, I figured that's the best way to learn so I incorporate it into my solutions. Glad I can help! Mineloxer 187 — 3y
Ad

Answer this question