Button that gives you a Badge ¿?

Hello! I’m actually creating a very basic game in which the objective is to find the right button inside a big room with buttons to receive a badge. I’m completely new scripting and everything that is out of building in Roblox Studio. The buttons only contain a ClickDetector and a couple of scripts that are responsible for changing the color of the button when you press it and another that allows you to hear a sound every time you click it. What can I do to receive a badge when I click on the correct button? I need a good explanation to understand and learn… :sweat_smile: Thank you very much!

  • Gonal
Just a side note: no need to sign your post. Your username is already on your posts!

You use the BadgeService:AwardBadge to award badges. You can look on the api reference page for badgeservice for other methods that might be useful to you.

7 Likes

You could either use a ClickDetector or you could have a script that uses the Part.Touched() event. You would also have to find the player who touched / clicked it

Here is an example:

Click button

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

Button = workspace.Button
ClickDetector = Button.ClickDetector

BadgeId = 123 -- the ID of your badge

Button.ClickDetector.MouseClick:Connect(function(Player)
    BadgeService:AwardBadge(Player.UserId, BadgeId)
end)

Touch Button

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

Button = workspace.Button

BadgeId = 123 -- the ID of your badge

Button.Touched:Connect(function(hit)
	if hit.Parent then
		local Player = PlayerService:FindFirstChild(hit.Parent.Name) -- checks if it was a player that touched it
		if Player then
			BadgeService:AwardBadge(Player.UserId, BadgeId)
		end
	end
end)
7 Likes

The canonical function to use here is GetPlayerFromCharacter. Please use this over FindFirstChild in the Players service with the character’s name.

5 Likes

Yes but what if an NPC touches it? the script will just return with an error, cause the NPC isn’t a player. Unless you add in a check function or something

False. GetPlayerFromCharacter does not error with an NPC as the parameter, it just returns nil.

2 Likes

for me, i get an error when i try to do that.

That’s probably because you have to change the parameter from hit.Parent.Name to hit.Parent since the parameter for GetPlayerFromCharacter is an instance and not a string.

3 Likes