Event Badge System

hello guys, I want to develop the following system. If you have collected 100 cash in game A you get a badge. In addition you get 10k diamonds in game B.

How to do that?

2 Likes

This has a risk of failure, as I didn’t want to put a Pcall function into it. If you want it, you may look here

local BadgeService = game:GetService("BadgeService")
local Value = ...
local BadgeId =  ...
Value:GetPropertyChangedSignal("Value"):Connect(function(x) 
 if x >= 100 and BadgeService:UserHasBadgeAsync(Player.UserId, BadgeId) ~= true then
  BadgeService:AwardBadge(Player.UserId, BadgeId)
 end
end)
2 Likes

And what to add in the other game where the player gets 10k money than. When the badge is awarded?

1 Like

You could try and use BadgeService:UserHasBadgeAsync() when the player joins the place, since it will return false if the player doesn’t have the badge.

2 Likes

Just gonna piece together what others have already said, fixing a few bits.

Game A:

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

local connections = {} --gonna help prevent memory leaks

local badgeId = 000000 --badge ID here

local function giveBadge(player:Player)
    local success, owned = pcall(bs.UserHasBadgeAsync, bs, player.UserId, badgeId)
    if not success then warn(owned) end
    
    local success, result = pcall(bs.AwardBadge, bs, player.UserId, badgeId)
    if not success then
        warn(result)
    end
end

local function onPlayerAdded(player:Player)
    --assuming the stat is in leaderstats
    local cash = player:WaitForChild("leaderstats"):WaitForChild("Cash") --modify these to however u made them in the player
    connections[player.Name] = cash:GetPropertyChangedSignal("Value"):Connect(function()
        if cash.Value >= 100 then
            giveBadge(player)
            onPlayerRemoving(player)
        end
    end)
end

function onPlayerRemoving(player:Player)
    local connection = connections[player.Name]
    if typeof(connection) == "RBXScriptConnection" then
        connection:Disconnect()
    end
end

players.PlayerAdded:Connect(onPlayerAdded)
players.PlayerRemoving:Connect(onPlayerRemoving)

Game B:

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

local badgeId = 000000 --badge ID here

local function checkForBadge(player:Player)
    local success, owned = pcall(bs.UserHasBadgeAsync, bs, player.UserId, badgeId)

    if not success then
        player:Kick("Failed to fetch badge data.")
    end

    if owned then
        print("Player has badge")
    end
end

players.PlayerAdded:Connect(checkForBadge)
2 Likes

Its nice but don’t spoon feed people. They won’t learn much otherwise usually.

I was providing code examples because the others didn’t and it was kind of vague or the code they provided was slightly incorrect/not great (no pcall, etc.), not to mention it will need to be adapted anyway.

Please don’t bump old topics with useless replies…

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.