Need help with Awarding Badge

So i have a loot box that awards player a certain tool as a reward and it has 4 items in it. Each item has their own rarity so basically i want to award player badge if they get the rarest item i have no idea how i can do that. So in the code below the one of the rarest item is neon teddy:

math.randomseed(tick())

script.Parent.Touched:connect(function(hit)

if hit.Parent:FindFirstChild("Humanoid") then	
	script.Disabled = true
	
local player = hit.Parent.Name 

local num = 0
for i = 1,45 do
	wait()
	script.Parent.CFrame = ( CFrame.new(script.Parent.Position) * CFrame.Angles(0,math.rad(num),0) )
	script.Parent.CFrame = ( CFrame.new(script.Parent.Position) * CFrame.Angles(0,math.rad(num),0) )
	num = num + 8
	
end
num  = 0


local random = math.random(1,100)		
	
if random > 0 and random < 10 then	
local reward1 = game.ServerStorage.NeonTeddy:Clone()
    reward1.Parent = 	game.Players[player].Backpack	

elseif	random > 10 and random < 20 then	
    local reward2 = game.ServerStorage.Unicorn:Clone()
    reward2.Parent = 	game.Players[player].Backpack	

elseif	random > 20 and random < 60 then	
    local reward3 = game.ServerStorage.ChocolateChipCookie:Clone()
    reward3.Parent = 	game.Players[player].Backpack

elseif	random > 60 and random < 100 then	
    local reward3 = game.ServerStorage.Teddy:Clone()
    reward3.Parent = 	game.Players[player].Backpack
end

script.Parent:Destroy()

end

end)
1 Like
local badge_service = game:GetService("BadgeService")
local BADGE_ID = 0 --//Input badge ID here

if not badge_service:UserOwnsBadgeAsync(player.UserId, BADGE_ID) then
badge_service:AwardBadge(player.UserId, BADGE_ID)
end

This is how you can use BadgeService to give a badge to the specified user ID. You could stick that code segment in your if statement block of code for awarding the rarest item. :slight_smile:

Also note you can only use :AwardBadge() server-sided for obvious reasons.

1 Like

will the code work if its in the workspace?

Server scripts run in Workspace and ServerScriptService, so yes.

1 Like

Like this? :

if random > 0 and random < 10 then	
    local reward1 = game.ServerStorage.NeonTeddy:Clone()
        reward1.Parent = 	game.Players[player].Backpack
    local badge_service = game:GetService("BadgeService")
    local BADGE_ID = 2124547286 --//Input badge ID here

    if not badge_service:UserOwnsBadgeAsync(player.UserId, BADGE_ID) then
    badge_service:AwardBadge(player.UserId, BADGE_ID)
    end

This is almost right, except this.

reward1.Parent = 	game.Players[player].Backpack

This implies that player is a string, not a player object. If this is the case, you can’t do player.UserId since it isn’t a player instance.

So what shall i do? 30 characters

if random > 0 and random < 10 then	
    local reward1 = game.ServerStorage.NeonTeddy:Clone()
        reward1.Parent = 	game.Players[player].Backpack
    local badge_service = game:GetService("BadgeService")
    local BADGE_ID = 2124547286 --//Input badge ID here

    if not badge_service:UserOwnsBadgeAsync(game.Players[player].UserId, BADGE_ID) then
    badge_service:AwardBadge(player.UserId, BADGE_ID)
    end

didnt work just tested 30 characters

Try this

math.randomseed(tick())

script.Parent.Touched:connect(function(hit)

if hit.Parent:FindFirstChild("Humanoid") then	
	script.Disabled = true
	
local player = hit.Parent.Name 

local num = 0
for i = 1,45 do
	wait()
	script.Parent.CFrame = ( CFrame.new(script.Parent.Position) * CFrame.Angles(0,math.rad(num),0) )
	script.Parent.CFrame = ( CFrame.new(script.Parent.Position) * CFrame.Angles(0,math.rad(num),0) )
	num = num + 8
	
end
num = 0

local function RarestItem()
    local BadgeService = game:GetService("BadgeService")
    local BadgeID = 2124547286

    if not BadgeService:UserOwnsBadgeAsync(game.Players[player].UserId, BadgeID) then
    BadgeService:AwardBadge(game.Players[player].UserId, BadgeID)
    end
end

local random = math.random(1,100)		
	
if random > 0 and random < 10 then	
local reward1 = game.ServerStorage.NeonTeddy:Clone()
    reward1.Parent = game.Players[player].Backpack	
    RarestItem()

elseif random > 10 and random < 20 then	
    local reward2 = game.ServerStorage.Unicorn:Clone()
    reward2.Parent = game.Players[player].Backpack	

elseif random > 20 and random < 60 then	
    local reward3 = game.ServerStorage.ChocolateChipCookie:Clone()
    reward3.Parent = game.Players[player].Backpack

elseif random > 60 and random < 100 then	
    local reward3 = game.ServerStorage.Teddy:Clone()
    reward3.Parent = game.Players[player].Backpack
end

script.Parent:Destroy()

end

end)