Badge for X5 wins

For a while now I’ve been trying to make a badge that is given when a player leaderstat (Wins) = 5

Nothing I’ve done has worked, so the help would be appreciated!

To pull this off, you gotta check if the player hits a certain number, which in case is 5 (leaderstat.Wins.Value == 5). If that’s the case, you can check using UserHasBadge to check if the player already has the badge. If the player doesn’t have the badge, use AwardBadge to give the player the badge.

1 Like

okay i will try doing that!

would it look like

if leaderstats.Wins.Value == 5 then
local BadgeService = game:GetService("BadgeService")
local BadgeId = (Badge)
 
game:GetService("Players").PlayerAdded:Connect(function(player)
local function awardBadge(player, badgeId)
    	if BadgeService:IsLegal(badgeId) and not BadgeService:IsDisabled(badgeId) then
    		BadgeService:AwardBadge(player.UserId, badgeId)
    	end
    end

Depricated and unnecessary.

Instead, I would do something like this:

game:GetService("Players").PlayerAdded:Connect(function(player)

local function awardBadge(player, badgeId)
    	if BadgeService:GetBadgeInfoAsync(BadgeId).IsEnabled == true then
    		BadgeService:AwardBadge(player.UserId, badgeId)
    	end
    end

more about BadgeService here

EDIT: Also if this is a new badge you will be adding to the game, I suggest doing
if leaderstats.Wins.Value >= 5 then … to make sure everyone gets the badge.

2 Likes

I would suggest you to use .Changed event for the wins, and check if the wins value is equal to 5, and if it is, awards the player the badge.
Something like this:

wins.Changed:Connect(function()
   --Award the player the badge.
end)
1 Like

so then like

wins.Changed:Connect(function()
if wins >= 5 then
--give badge
end)
1 Like

You can combine the creation of leaderstats and the Wins Changed even to award the badge. You can do this as follows:

local badgeService = game:GetService("BadgeService")
local badgeID = 69420
game:GetService("Players").PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new("StringValue")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player
    local wins = Instance.new("IntValue")
    wins.Name = "Wins"
    wins.Parent = leaderstats
    --Sets Up leaderstats

    wins.Changed:Connect(function()
        if wins.Value == 5 then
            if BadgeService:GetBadgeInfoAsync(badgeID).IsEnabled == true then
    		    BadgeService:AwardBadge(player.UserId, badgeID)
    	    end
        end
    end)
    --Award Badge when Wins Changes
end)

I set up the leaderstats and the Changed Signal for you.

i already have the wins leaderstat so you dont need to worry about that (thanks anyways!) ima test now

found a mistake when you said

local badgeService = game:GetService("BadgeService") 

you then said BadgeService when using variables so just a capitalization error!

would this work? (I have no way of knowing since i already own the badge)

local badgeService = game:GetService("BadgeService")
local badgeID = 2124542294
game:GetService("Players").PlayerAdded:Connect(function(player)

    player.leaderstats.wins.Changed:Connect(function()
        if player.leaderstats.wins.Value >= 5 then
			print("Sucess")
            if badgeService:GetBadgeInfoAsync(badgeID).IsEnabled == true then
    		    badgeService:AwardBadge(player.UserId, badgeID)
    	    end
        end
    end)
end)

Delete the badge from your inventory!

good idea wont that delete badge though?

like from the game entirely? :woozy_face:

oh wait it isnt in my inventory

i playtested and in output it said
14:42:46.957 - Workspace.Starter.Scripts.Won X5:5: attempt to index nil with ‘leaderstats’

wassat mean?

tampered with script so have a look at it line 5

local badgeService = game:GetService("BadgeService")
local badgeID = 2124542294
game:GetService("Players").PlayerAdded:Connect(function(player)

    game.Players.LocalPlayer.leaderstats.Wins.Changed:Connect(function()
	print("Trying")
        if game.Players.LocalPlayer.leaderstats.Wins.Value >= 5 then
			print("Sucess X5 WINS")
            if badgeService:GetBadgeInfoAsync(badgeID).IsEnabled == true then
    		    badgeService:AwardBadge(player.UserId, badgeID)
    	    end
        end
    end)
end)

Why do you need the local player? In the function you have the player already.
And you can’t use the local player in a server script.

wait so it goes in serverscriptservice?

ok now it looks like this

local badgeService = game:GetService("BadgeService")
local badgeID = 2124542294
game:GetService("Players").PlayerAdded:Connect(function(player)

    player.leaderstats.Wins.Changed:Connect(function()
	print("Trying")
        if player.leaderstats.Wins.Value >= 5 then
			print("Sucess X5 WINS")
            if badgeService:GetBadgeInfoAsync(badgeID).IsEnabled == true then
    		    badgeService:AwardBadge(player.UserId, badgeID)
    	    end
        end
    end)
end)
2 Likes

I think it will still say that the leaderstats is nil, because you create it from another script, and at the same time try to find it from this script. I suggest you to use them in the same script, and you also have your wins variable there, so you can use .Changed without it’s path. And I am almost sure that it needs to be a server script, you’ve made it local before. Try to add this script to the one that creates the wins value.