My Badge Script refuses to give badges stating that the badge is nil, any help?

Im attempting to give the player a badge when a certain part is clicked, then cloning the weapon into their backpack/startergear. When the code is executed, everything runs except the badge giver.
The badge giver refuses to award the badge.

Here is the code:

local badgeService = game:GetService("BadgeService")
local saberBadgeId = 2124589090

local debounce = false
local saberCollect = game.Workspace.Crossguard.CrossguardCollect
local clickDetect = saberCollect.ClickDetector
local whispers = saberCollect.Whisper

local function awardBadge(player,badgeId)
	local success, failure pcall(function()
		badgeService:AwardBadge(player.UserId,badgeId)
	end)
			
	if not success then
		warn("error occured whilst awarding badge: ", failure)
	end		
end

clickDetect.MouseClick:Connect(function(player)
	if debounce then return end
	debounce = true
	whispers:Play()
	wait(5)
	whispers:Stop()
	if player.Backpack:FindFirstChild"Crossguard Lightsaber" or player.StarterGear:FindFirstChild"Crossguard Lightsaber" then debounce = false return end
	awardBadge(player, saberBadgeId)
	local x = game.ServerStorage["Crossguard Lightsaber"]:Clone()
	local y = x:Clone()
	x.Parent = player.StarterGear
	y.Parent = player.Backpack
	player.Sabers.Crossguard.Value = true
	debounce = false
end)

Your syntax is incorrect here. It should be …failure = pcall(function()

Turns out it runs properly when doing this but it never declares those two variables properly so whatever is in the statement below doesn’t ever execute because Lua assumes you’re creating two empty variables success and failure then running a pcall function right after it, not actually giving them any value, so success and failure would always be nil, hence it would always error and return the error as nothing or nil.

1 Like

saberBadgeId = 2124589090
(player.UserId, badgeId)

You wrongfully named the variable??

That is just the parameter for the awardBadge function. You can see below when he calls it, that he passes the correct variable.

What you actually did wrong is that you did this

local success, failure pcall(function()

end -- This is not correct
--Now this below is the right one

local success, failure = pcall(function()

end

I’ve tested it out, and as it turned out, the mistake was that you forgot the equals sign. So yeah, what Evercyan said was right to begin with.

Also, in this right here:

Instead of doing that, you should do this,

if success then 
print("Sucessful")
elseif failure then
print(failure)

else
print("Nil Value")
end


That way, you can find future errors.

1 Like