How To Grant A Badge When A Player Gets Enough Of An Ingame Currency?

How To Grant A Badge When A Player Gets Enough Of An Ingame Currency ?

When A Player Gets 100 Of An In game Currency For Example Candy ? When They Get 100 Of It How Can I Reward A Badge ?

2 Likes

I cannot give you an script because that’s against the rules, but you could detect when the currency amount increases, check if it’s over or equal to 100 now and if it is grant the badge.

2 Likes

This is localscript can place in StarterPlayer —> StarterPlayerScripts.
a little bit of example:

local bs = game:GetService("BadgeService") -- the badgeservice.
local plr = game.Players.LocalPlayer -- gets the player in the script.
local Candy = plr:WaitForChild("leaderstats"):FindFirstChild("YourValueNameHere") -- finds leaderstats folder inside of player. and the second one. is the value! Example. Like your value is "Candy" or "Money" Chnage that to your value name.
if plr.Candy.Value == 100 then -- if player value hits to 100. it will catch up! the second you have to type the badge you want to give. 
-- do stuff
end
3 Likes

This means it only awards when the cash is exactly 100.

2 Likes

yes, and what if lets say they have like 99 coins, and get 2 coins at once then they have 101 coins, and they wont get the badge

1 Like

Make sure you have a leaderstat folder with an IntValue or NumValue that holds this currency.

You’d want to use an RBXScriptConnection signal, that is .Changed. For this instance, I’ll use NumberValue.Changed:

local badgeService = game:GetService("BadgeService") -- service that handles badges
local players = game:GetService("Players")

players.PlayerAdded:Connect(function(player)

    -- your leaderstats code here

    numValue.Changed:Connect(function(value) -- define numValue by the numValue you create in the leaderstat folder
        -- this detects the change to the numValue's value property
    end)

end)

Now, we’d use the if statements to check if the player has enough currency. Let’s create another variable that holds a dictionary of badges, with a number needed to exceed to be rewarded.

local badgeService = game:GetService("BadgeService")
local players = game:GetService("Players")

local badges = {
    -- [number needed] = badgeId
    [100] = 0000,
    [200] = 0000,
    -- so on.
}

players.PlayerAdded:Connect(function(player)

    -- ...

    numValue.Changed:Connect(function(value)
    
    end)

end)

Then just iterate through the dictionary, and reward the player with the corresponding ID if they meet the requirement of the index, which in this case, is the number needed.

numValue.Changed:Connect(function(value)

    for numberNeeded, badgeId in pairs(badges) do
        if numberNeeded <= value and not badgeService:UserHasBadgeAsync(player.UserId, badgeId) then
            badgeService:AwardBadge(player.UserId, badgeId)
        end
    end

end)
2 Likes
local players = game:GetService("Players")
local badges = game:GetService("BadgeService")
local badgeId = 0 --change to badge id

players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder") --leaderstats
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	local candy = Instance.new("IntValue") --candy stat of leaderstats
	candy.Name = "Candy"
	candy.Value = 0
	candy.Parent = leaderstats
	candy.Changed:Connect(function(newVal)
		if newVal >= 100 then --award badge for 100 candies
			badges:AwardBadge(player.UserId, badgeId)
		end
		if newVal >= 200 then --award badge for 200 candies
			badges:AwardBadge(player.UserId, badgeId)
		end --follow this format to add more badges
	end)
end)
2 Likes

where would this go ? in the scripts ?

The ServerScriptService folder, it is a server script.

where do i go from this ? i put it in a script in server script service does that mean its working
if the thing on leader board is called Candy ?
also on this script do i make several for separate badges can you explain it a bit more im really struggling here

Yes, that means it’s working, to add new badges you just have to use the same format which the two current badges use:

candy.Changed:Connect(function(newVal)
	if newVal >= 100 then --award badge for 100 candies
		badges:AwardBadge(player.UserId, badgeId)
	end
	if newVal >= 200 then --award badge for 200 candies
		badges:AwardBadge(player.UserId, badgeId)
	end --follow this format to add more badges
end)

Whenever the candy stat changes the “Changed” event is fired and the connected function executes, the function then performs checks to see if the value of the candy stat is above a specified amount in this case this is done two times (first 100 then 200) and if either evaluate as true then the corresponding badge is awarded.

1 Like
if newVal >= 0 then --award badge for 0 candies
	badges:AwardBadge(player.UserId, badgeId)
end --follow this format to add more badges

So this block of code here, just add it with the others to add more badges (and change the value from 0).

1 Like

Still Confused and code isn’t working

my candy is on the leader board as Candy and its in a script in workspace that makes a new instance and a folder and a leader stats named Candy.
The Code doesn’t grant the badge. and I don’t get the adding more when it only has 1 id area on it ?
can you go over it a bit more ?

How would you fix that code where it would grant correctly ?

1 Like

You need to change the badge ID for it to award a badge. If I were you I’d just stick to using free models.

instead of ==, i’d just use >= 100 or > 99

wdadadawdad

  1. Would this work for 750 candies ?

  2. where is the ids for the other do you copy and paste the code several times ?

Yes that would work, if you want multiple badges you can change the script from:
local badgeId = 0 --change to badge id
To something like:

local badgeId1 = 0 --change to badge id
local badgeId2 = 0 --change to badge id
badges:AwardBadge(player.UserId, badgeId1)
badges:AwardBadge(player.UserId, badgeId2)
1 Like