Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

My text button that awards a badge won't work?

I am trying to make a "badge walk" style game, except instead of walking for a long amount of time, you just click a TextButton. However, my code doesn't work, and nothing gets written into the console.

local button = script.Parent
local BadgeService = game:GetService("BadgeService") -- this gets the service used for badges
local PlayedBadge = 2124637741 -- replace the numbers with your badge id found after you create it
local function onButtonActivated(player)
	print("this should work")
	-- 1
	BadgeService:AwardBadge(player.UserId, PlayedBadge)
end

button.Activated:Connect(onButtonActivated)

I am using a regular script. I have followed many tutorials, but none seem to work.

Update: Because of one of the answers, I am now using this script.

local button = script.Parent
local BadgeService = game:GetService("BadgeService") -- this gets the service used for badges
local PlayedBadge = 2124637741 -- replace the numbers with your badge id found after you create it
local function onButtonActivated(player)
	print("this should work")
	-- 1
	BadgeService:AwardBadge(player.UserId, PlayedBadge)
end

--// mouse click!
button.MouseButton1Click:Connect(onButtonActivated)
 
--// touch
button.TouchTap:Connect(onButtonActivated)

The TextButton works, but now I get an error: 19:15:57.265 - Players.efan343.PlayerGui.SurfaceGui.TextButton.Script:7: attempt to index nil with 'UserId' Also, because apparently, it matters for some reason, it is a script inside a TextButton inside a SurfaceGui inside StartedGui.

2 comments

0

TextButton.MouseButton1Click doesn't send the player as a parameter I believe.

0

How do you make the player be sent?

4 answers

You have the wrong event "Activated" event on a textbutton only fires when you set the property of the button Active = true (Guestimate!)

The event you want is called MouseButton1Click or for touch sensitive devices TouchTap

I would change your script to the following:

local button = script.Parent
local BadgeService = game:GetService("BadgeService") -- this gets the service used for badges
local PlayedBadge = 2124637741 -- replace the numbers with your badge id found after you create it
local function onButtonActivated(player)
    print("this should work")
    -- 1
    BadgeService:AwardBadge(player.UserId, PlayedBadge)
end

--// mouse click!
button.MouseButton1Click:Connect(onButtonActivated)

--// touch
button.TouchTap:Connect(onButtonActivated)

Hope this helps! :)

2 comments

0

Well now, I'm getting an error in the console: "Players.efan343.PlayerGui.SurfaceGui.TextButton.Script:7: attempt to index nil with 'UserId'"

0

This means that UserId does not exist. If I'm not mistaken there is no variable that's passed through the MouseButton1Click function. Instead declare the player with "local player = game.Players.LocalPlayer" and it should work.

you cant earn a badge in studio, im sure you havent tried to publish the update and test it on roblox site, so go ahead and do that

4 comments

0

I know, but using it on the Roblox site won't help.

0

you cant get the badge in studio, only the site allows for badge award so test in-game instead of in-studio

0

Yes, I tested it through normal Roblox instead of Roblox Studio. It still doesn't work.

0

put it in a pcall, there you will see if it fails local result, error= pcall(function() --award badge here end) print(result,error) 

Try it a different way, something like this should work.

local button = script.Parent
local BadgeService = game:GetService("BadgeService")
 
local function onButtonActivated(player, 2124637741) --This is the badge Id
	local success, badgeInfo = pcall(function()
		return BadgeService:GetBadgeInfoAsync(2124637741)
	end)
	if success then --This confirms if the badge can be rewarded.
		if badgeInfo.IsEnabled then
			local awarded, errorMessage = pcall(function()
				BadgeService:AwardBadge(player.UserId, badgeId)
			end)
			if not awarded then --If the badge was not awarded then it should tell you the error.
				warn("There was a badge error:", errorMessage)
			end
		end
	else
		warn("Error while getting badge info")
	end
end

button.MouseButton1Click:Connect(onButtonActivated)

Hope this helps!

3 comments

0

I need to be able to expand this easily.

0

? Sorry do not know what that means.

0

I am planning to add more badges.

Only the server can award a badge. Make sure you are using a remote event to send info to the server to award the badge.

I'm assuming that you are using a local script since you are doing mousebutton1click.

If you aren't using a local script then that's also an issue, mouse click only works on the client.

One last thing: Awarding a badge through client activation may be unsafe in most cases, exploiters can gain access nearly immediately through this (thats because its the client). the best way to award badges is through touch events or simple automation on the server. I don't know the context though so im assuming you are using it in a way were exploiters obtaining the badge wont matter.

1 comment

0

It is in a way that exploiters getting the badge doesn't matter, the badge is free anyways. (Read the OP.) Also, I am using a server script, but mousebutton1click works, since "this should work" is printed into the console and an error is given.

This discussion is preserved as it was. New answers, comments, and votes are closed.