How to give a badge

I made a game and I want to add a badge script on part touched.
I’ve tried multiple scripts and none work.
How can i do this with a script.
Game is 1337: A btools game - Roblox
Badge is The Egg - Roblox

11 Likes

Boop

For your Instance, you can use the Touched event to detect when a Player hits the Part

local Part = script.Parent
local IDToAward = 000000 --Replace this with your BadgeID
local BadgeService = game:GetService("BadgeService")

Part.Touched:Connect(function(Hit)
    local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
    if Player then
        BadgeService:AwardBadge(Player.UserId, IDToAward)
    end
end)

Hit will return back the Part that it hit, and Hit.Parent would be referring to the Player’s Character in this Instance so we can use the GetPlayerFromCharacter function which will return back a Player object if a Player has been found

Also you spelt badge wrong in your OP LOL

13 Likes

You may want to implement a debounce.

1 Like
local badgeservice = game:GetService("BadgeService")
local id = 0000000000 --badge id here

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

	if hit.Parent:FindFirstChild("Humanoid")then

		local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
		badgeservice:AwardBadge(plr.UserId,id)
	end
end)

you can try using this script so it will give the player a badge once they touch the part

hopefully this is probably what you are looking for

4 Likes

If any thing with a humanoid touches it, it will try to give a badge to anything. I would check if the character is from a real player before awarding.

I just only gave a brief example on how it could be implemented & the basics on how Touched works, but alrighty

Say you want to award the badge to players that don’t have it

What we can do is use UserHasBadgeAsync and check if the Player does have the badge or not, if they do then we can just pass our script like nothing happened

But if they don’t, then we can reward them:

local Part = script.Parent
local IDToAward = 000000 --Replace this with your BadgeID
local BadgeService = game:GetService("BadgeService")

Part.Touched:Connect(function(Hit)
    local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
    if Player then
        if not BadgeService:UserHasBadgeAsync(Player.UserId, IDToAward) then
            BadgeService:AwardBadge(Player.UserId, IDToAward)
        end
    end
end)

I don’t really know how you’d implement a debounce in this Instance unless if you’re wanting to make the part wait 5 seconds before firing again

3 Likes

Ill implement this and see if it works

what was that removed reply about

This could lag your game and throw a error. You need to have a cool down so that it won’t attempt to award a badge 100 times.

1 Like

Basically i used this script local BadgeService = game:GetService(“BadgeService”)
local Players = game:GetService(“Players”)

local badgeID = 2124707470

local function awardBadge()

local player = Players.LocalPlayer
local hasBadge = false

-- Check if the player already has the badge
local success, message = pcall(function()
	hasBadge = BadgeService:UserHasBadgeAsync(player.UserId, badgeID)
end)

-- If there's an error, issue a warning and exit the function
if not success then
	warn("Error while checking if player has badge: " .. tostring(message))
	return
end

if hasBadge == false then
	BadgeService:AwardBadge(player.UserId, badgeID)
end

end

1 Like

Not sure if the script you’re using is an Outdated script (Since you’re referencing the LocalPlayer)

2 Likes

I am going to test this in studio first

Don’t use the old one. Try this one now:

local Part = script.Parent
local IDToAward = 2124707470 --Replace this with your BadgeID
local BadgeService = game:GetService("BadgeService")
local runSerivce = game:GetService("RunService")
local debounce = true
Part.Touched:Connect(function(Hit)
	local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
	if Player then
		if not BadgeService:UserHasBadgeAsync(Player.UserId, IDToAward) and debounce then
			debounce = false
			BadgeService:AwardBadge(Player.UserId, IDToAward)
		end
	end
end)

while wait() do
	if debounce == false then
		wait(5) -- Cooldown Time
		debounce = true
	end
end
1 Like

Ok, I will, thank you for the script, I’m assuming this is a local script.

Put this in a normal script in the part you want to touch.

Why is the debounce in a loop? Can’t you just combine it with the Touched event?

Something like this is simpler and does the job

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

local Part = script.Parent
local IDToAward = 2124707470 --Replace this with your BadgeID

local debounce = true

Part.Touched:Connect(function(Hit)
	local Player = Players:GetPlayerFromCharacter(Hit.Parent)
	if not Player or BadgeService:UserHasBadgeAsync(Player.UserId, IDToAward) or not debounce then
		return
	end
	debounce = false
	BadgeService:AwardBadge(Player.UserId, IDToAward)
	wait(5)
	debounce = true
end)

Regular script in the part that gives the badge

8 Likes

Each touched event is on a separate thread. It’s like making a new script and running it. Each wait will be different from each other.

1 Like

This script works and Ill credit this thread in the description of my game. Thank you :smiley:

1 Like

And? It’s going to treat the debounce variable the same even if multiple touch events happen, that’s how you normally do a debounce with events, with a variable and yielding.

Yes they technically are on a new thread each time they’re fired, but they still see variables the same

@3prm3 Glad that I’ve helped you! I recommend you mark a post as the solution so others will know it is solved! If you have anymore issues don’t be afraid to make another post!

Bruh you got the solution? I posted mine first and it worked fine. I also was the one to bring up the debounce in the first place.