Need help with a badge giver script

I’m currently attempting to script a badge-giver that gives a badge when you click a part. How would I go about achieving this? I’ve tried the developer site thing with the badges but cant seem to figure it out.
Any help?

3 Likes
  1. Add a ClickDetector inside your part

  2. Add a Script and put this in it

local BadgeID = 123456789  -- type the badge id here

game.Players.PlayerAdded:Connect(function(Player)
	script.Parent.ClickDetector.MouseClick:Connect(function()
		local BadgeService = game:GetService("BadgeService")
		BadgeService:AwardBadge(Player.UserId, BadgeID)
	end)
end)
4 Likes

Little tip, the first parameter which is inherited by the mouse click event is the player who clicked it so instead of doing that you could just do

local BadgeID = 123456789  -- type the badge id here

	script.Parent.ClickDetector.MouseClick:Connect(function(Player)
		local BadgeService = game:GetService("BadgeService")
		BadgeService:AwardBadge(Player.UserId, BadgeID)
	end)
3 Likes

Oh! I actually didnt know that

thank you for pointing that out

1 Like

I tried this but it didnt seem to work. I get no output errors either. Heres an image of my script.

Desktop Screenshot 2021.03.27 - 20.42.53.36 (2)

2 Likes

I am honestly not familiar with the badge service api i just know that you can get the player who clicked it with that. Try seeing if the badge is activated as i heard you have to pay for it now

Yeah Ive paid for the badge, this is very odd. You can find almost 0 badge tutorials with click detectors, which really sucks.

oh! you put the mouseclick function inside your onClicked function

just remove the script.Parent.ClickDetector.MouseClick:Connect(function(Player) and the end) and youll be good

Oh I just looked on the wiki I think we found the problem

" * The player must not already have the badge (note that a player may delete an awarded badge from their profile and be awarded the badge again)."

Try running this in the console

game:GetService(“BadgeService”):UserHasBadgeAsync(451731998, 2124711166)

Now the “player.UserId” has a red line under it, error is W001: Unknown global “Player”

add a player parameter inside ur onclicked function so like

local function onClicked(Player) would work

You should try and figure out how to do this on your own before posting on the developer forum. This isn’t a script request site. We’re willing to help, we just want to see some code so we know you tried.

With that said, I recently was doing some work with badges and I have some code laying here. After I made some adjustments, I think this is what you’re looking for:

-- // In the part that you have to click. The click detector should also be in this part.

--------------------------
-- [[ Varible Define ]] --
--------------------------
-- // Services
local BadgeService = game:GetService("BadgeService")
local PlayerService = game:GetService("Players")
-- // Objects
local buttonPart = script.Parent
local clickDetector = buttonPart:FindFirstChildOfClass("ClickDetector")
-- // Numbers
local badgeID = 000 -- // Put the badge ID here
-- // Arrays
local badgePossession = {
	clickDetectorBadge = {},
}

---------------------------
-- [[ Function Define ]] --
---------------------------


local function AwardBadge(Plr, badgeId, stringIndex) -- // AwardBadge(Zyrun, the desired badge ID, the name of the badge in badgePossession)
	local stringUserId = tostring(Plr.UserId)
	local hasBadge = false
	if (badgePossession[stringIndex][stringUserId] ~= nil) then -- // If the cache says that the player has the badge
		hasBadge = badgePossession[stringIndex][stringUserId]
	else -- // If we don't have data on the player yet
		local success, errorMessage = pcall(function()
			hasBadge = BadgeService:UserHasBadgeAsync(Plr.UserId, badgeId)	
		end)
		if (not success) then
			warn("Error while checking if player has " .. stringIndex .. " badge: " .. errorMessage)
			return nil
		end
		badgePossession[stringIndex][stringUserId] = hasBadge -- // Add the player to the cache, even if they don't have the badge
	end
	if (hasBadge) then 
		return nil
	end
	local success, badgeInfo = pcall(function()
		return BadgeService:GetBadgeInfoAsync(badgeId)
	end)
	if (success) and (badgeInfo.IsEnabled) then
		local awarded, errorMessage = pcall(function()
			BadgeService:AwardBadge(Plr.UserId, badgeId)
		end)
		if (not awarded) then
			warn("Error while awarding " .. stringIndex .. " badge: " .. errorMessage)
		else
			badgePossession[stringIndex][stringUserId] = true
		end
	end
end

local function PlayerLeaving(Plr) -- // So that players can still function if they get the badge in a different server
	local stringUserId = tostring(Plr.UserId)
	for _, tableSet in pairs(badgePossession) do
		if (tableSet[stringUserId] ~= true) then
			tableSet[stringUserId] = nil
		end
	end
end

----------------------------
-- [[ Connector Define ]] --
----------------------------

PlayerService.PlayerRemoving:Connect(PlayerLeaving) -- // Player left the game
clickDetector.MouseClick:Connect(function(playerClicking) -- // Player clicked the click detector in our part
	AwardBadge(playerClicking, badgeID, "clickDetectorBadge")
end)

Ive been attempting this for an hour, once I gave up I came here. Sorry if I sound hostile, but I managed to figure it out with their help.

1 Like

I’m glad you were able to get a solution.

You should mark whoever posted the solution to your question as the solution so others know that you have found your answer and no longer need help.

Good luck in your game development!

1 Like