Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do you make a click part to get badge?

Asked by 2 years ago

I want to make a part where if you click it you get a badge, how can I do that?

0
Have you created a badge? Mathilinium 112 — 2y

1 answer

Log in to vote
0
Answered by
SuperPuiu 497 Moderation Voter
2 years ago

Hello there! I will answer and explain how to do that.

First, add a Script to a part and a ClickDetector. You can call the script whatever you want.

Now, open the script and get BadgeService and connect MouseClick event from ClickDetector

local BadgeService = game:GetService("BadgeService") -- Badge service, used for awarding badges
local badgeId = 1 -- Change 1 to the badge id

script.Parent.ClickDetector.MouseClick:Connect(function(Player)

end)

Now, we will use pcall for a protected call and we will use AwardBadge from BadgeService

local success, result = pcall(function()
        return BadgeService:AwardBadge(player.UserId, badgeId)
    end)
    if success then
        print("Success!")
    else
        warn(result) -- If it fails, used for debug
    end

Your code should look like that:

local BadgeService = game:GetService("BadgeService")
local badgeId = 1 -- Change 1 to badge id

script.Parent.ClickDetector.MouseClick:Connect(function(player)
    local success, result = pcall(function()
        return BadgeService:AwardBadge(player.UserId, badgeId)
    end)
    if success then
        print("Success!")
    else
        warn(result) -- If it fails, used for debug
    end
end)

BadgeService

0
You should explain the code a bit more. The questioner might not understand what pcalls() are, or why you used return. MarkedTomato 810 — 2y
0
My bad, sorry. SuperPuiu 497 — 2y
0
Thank you so much! I don't have much knowledge about pcalls, but hey, I guess this is my chance to learn! :) Ghostinee 27 — 2y
Ad

Answer this question