How can I award a badge to a player when they unlock a certain leaderstat value?

Im currently working on a fighting game, and when a player gets a certain amount of kills it awards a badge for them. I tried doing this but I couldn’t figure it out. Heres the script:

Local badge = game:GetService(“BadgeService”)

game.LeaderBoard.Stat = leaderstat

leaderstat.Name = (“Kills”)

local Kills = 10

game:GetService(“Value”)

Value.Name = ammountofKills

ammountofKills.Value = 10

when ammountofkills.Value.10 = true then

game.RewardBadge.Player

Badge Id = “rbxassetid//0”

when ammountofkills.Value.10 = false then

RewardBadge = false

     end
end)

This is hard when you are trying to make a thing where the player gets a badge rewarded to them. I have seen it in a lot of games, but I do not know how to technically make one.

5 Likes

Pay 500 robux for a badge, then read API about badge rewards.

1 Like

Wait no, im dumb, just do thing like

Kills.Value.Changed:Connect(function()
if Kills.Value >= 10 then 
--reward here
end
end)
2 Likes

For any leaderstat value. I put kills as an example. The kills ammount is the value of the example I did.

1 Like

Just change kills to any leaderstats value. Use .Changed event to trigger changes.

1 Like

Hmmm, it should not be to hard, but you can get the value of your leader stat by doing this game.LeaderBoard.Kills.Value and make a function checking if the player should receive the badge everytime the number is changed
game.LeaderBoard.Kills.Value.Changed:Connect(function()
if game.LeaderBoard.Kills.Value>5 then
badge award code here
end
end)
Edit:znimator solved it before me, you win lol.

1 Like

ITS ZNIMATOR. Btw yes. Changed event would be always helpful.

2 Likes

Just probably do:

local BadgeService = game:GetService("BadgeService")

Kills:GetPropertyChangedSignal("Value"):Connect(function(newValue)
    if newValue >= 10 then --The amount that you want to give badge at.
       BadgeService:AwardBadge(yourBadgeIdHere)
    end
end)
2 Likes

The event Instance:GetPropertyChangedSignal() does not give the new value, but the IntValue.Changed event does.

1 Like

GetPropertyChangedSignal also does I think.

1 Like

It does not, please review the links I posted.

1 Like

The first thing we should do, is index our services so that we know what to index exactly. But for now, we can just index the BadgeService:

local BadgeService = game:GetService("BadgeService")

Next up, we’re gonna add a PlayerAdded event, which will fire whenever a player joins the current server and also give them their stats:

local BadgeService = game:GetService("BadgeService")

game.Players.PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    local Kills = Instance.new("IntValue")
    Kills.Name = "Kills"
    Kills.Parent = leaderstats
end)

Now, we’re gonna find the leaderstats using the FindFirstChild Instance and also check to see if the current player has met the correct requirements to be awarded the badge using the IntValue.Changed function (Not the IntValue.Value.Changed function:)

local BadgeService = game:GetService("BadgeService")

game.Players.PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    local Kills = Instance.new("IntValue")
    Kills.Name = "Kills"
    Kills.Parent = leaderstats

    repeat wait() until player:FindFirstChild("leaderstats") ~= nil
    player.leaderstats.Kills.Changed:Connect(function()
    		if player.leaderstats.Kills.Value >= 10 then
		BadgeService:AwardBadge(BadgeId, player.UserId)
		print("Badge awarded to"..player.Name.."!")
	end
end)
end)

BadgeId is just a placeholder for the Badge’s ID which can be located in your Game Explorer > Badges > Right Click Badge > Copy ID. Hope this helps! (This is also my first explanation)

2 Likes

Just use an if statement for the player’s kill value.

example :

if kill.Value >= (value) then
  --code
end

--or
local badgeserv = game:GetService("BadgeService")
local badgeid = -- your badge id

Kills:GetPropertyChangedSignal("Value"):Connect(function(value)
      if value >= (your amount) then
           BadgeService:AwardBadge(badgeid)
      end
end)
2 Likes

@JackscarIitt There is no reason to do repeat wait() until player:FindFirstChild("leaderstats") ~= nil, you should be doing player:WaitForChild("leaderstats"). The IntValue.Changed event gives the new value as the first parameter so you do not have to write the path again when checking.

@ANDROHERSEY00

2 Likes