Difrentiate between having or not having a badge





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

local badgeID = 2124809547  -- Change this to your badge ID

local function onPlayerAdded(player)
	

	-- Check if the player has the badge
	local success, hasBadge = pcall(function()
		return 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!")
		return
	end

	if hasBadge then
		local chr = script.Parent
		local part = game.ReplicatedStorage.GucciWatch:Clone() 
		part.Parent = chr
		local weld = Instance.new("Weld",chr)
		weld.Part1 = part.Union
		weld.Part0 = chr:WaitForChild("Left Arm")
		weld.C1 = CFrame.new(0, 0, .3) * CFrame.Angles(math.rad(90),0,0)
	else
		local chr = script.Parent
		local part = game.ReplicatedStorage.Watch:Clone() 
		part.Parent = chr
		local weld = Instance.new("Weld",chr)
		weld.Part1 = part.Union
		weld.Part0 = chr:WaitForChild("Left Arm")
		weld.C1 = CFrame.new(0, 0, .3) * CFrame.Angles(math.rad(90),0,0)
		
	end

	
end

-- Connect "PlayerAdded" events to the "onPlayerAdded()" function











local function awardBadge(player, badgeId)
	-- Fetch badge information
	local success, badgeInfo = pcall(function()
		return BadgeService:GetBadgeInfoAsync(badgeId)
	end)
	if success then
		-- Confirm that badge can be awarded
		if badgeInfo.IsEnabled then
			-- Award badge
			local awarded, errorMessage = pcall(function()
				BadgeService:AwardBadge(player.UserId, badgeId)
			end)
			if not awarded then
				warn("Error while awarding badge:", errorMessage)
			end
		end
	else
		warn("Error while fetching badge info!")
	end
end

game:GetService("ReplicatedStorage").Gucci.OnServerEvent:Connect(function(player)

	awardBadge(player,2124809547)
end)






Players.PlayerAdded:Connect(onPlayerAdded)



So i have a script that gives players a watch that they can stare at, Your first watch is broken but if you stare at it for 10 minutes, you get a slightly less dinky one and a badge to say that you have it.

For some reason when I try to use this script to decide which to give them, it just doesen’t give them any of them, anyone know why?

PCalls return two values, a boolean that is true if there wasn’t an error, and an error message if it did. If it didn’t error, that error message will be nil. You should instead change this to;

local hasBadge;
local success, errorMessage = pcall(function()
	hasBadge = BadgeService:UserHasBadgeAsync(player.UserId, badgeID);
end)

So what would that change in terms of differentiating the two? What would my next step be?

That would be your only problem. Your script should work now.

Odd, The watch still isn’t showing up. I’ll play with it a little

I tried changing how it detects which that i have, but it still gives me the default watch


local bruh = false


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

local badgeID = 2124809547  -- Change this to your badge ID

local function onPlayerAdded(player)
	
	local hasBadge;
	-- Check if the player has the badge
	local success, errorMessage = 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!")
		return
	end

	if hasBadge then
		bruh = true
		local chr = script.Parent
		local part = game.ReplicatedStorage.GucciWatch:Clone() 
		part.Parent = chr
		local weld = Instance.new("Weld",chr)
		weld.Part1 = part.Union
		weld.Part0 = chr:WaitForChild("Left Arm")
		weld.C1 = CFrame.new(0, 0, .3) * CFrame.Angles(math.rad(90),0,0)
	end

	
end

-- Connect "PlayerAdded" events to the "onPlayerAdded()" function





if bruh == false then
	local chr = script.Parent
	local part = game.ReplicatedStorage.Watch:Clone() 
	part.Parent = chr
	local weld = Instance.new("Weld",chr)
	weld.Part1 = part.Union
	weld.Part0 = chr:WaitForChild("Left Arm")
	weld.C1 = CFrame.new(0, 0, .3) * CFrame.Angles(math.rad(90),0,0)
end





local function awardBadge(player, badgeId)
	-- Fetch badge information
	local success, badgeInfo = pcall(function()
		return BadgeService:GetBadgeInfoAsync(badgeId)
	end)
	if success then
		-- Confirm that badge can be awarded
		if badgeInfo.IsEnabled then
			-- Award badge
			local awarded, errorMessage = pcall(function()
				BadgeService:AwardBadge(player.UserId, badgeId)
			end)
			if not awarded then
				warn("Error while awarding badge:", errorMessage)
			end
		end
	else
		warn("Error while fetching badge info!")
	end
end

game:GetService("ReplicatedStorage").Gucci.OnServerEvent:Connect(function(player)

	awardBadge(player,2124809547)
end)






Players.PlayerAdded:Connect(onPlayerAdded)



any idea why?

Oh wait i made a little goof at the top one second

Now the badge just doesen’t show up when i set local bruh = false to local bruh
:frowning:

I’m kinda stumped, to be honest. Try debugging with print statements. Make it print hasBadge before the if has badge statement

Alright i’ll see to that. Thanks for the help

1 Like

Yeah i’m not getting anywhere. No idea why this is happening