Trying to award a player a badge when they touch a specific part

I want to award a player a badge so that they get a badge for when they complete each level/difficulty of my game.
Every script I’ve tried doesn’t give the player the badge. I don’t know why this is happening.
I’ve tried these 3 scripts (1 at a time) and none of them work.

Script 1:

local BadgeService = game:GetService("BadgeService")
local id = 2140926703
local badgegive = script.Parent

badgegive.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
		if not BadgeService:UserHasBadgeAsync(plr.UserId, id) then
			BadgeService:AwardBage(plr.UserId, id)
		end
	end
end)

Script 2:

local Players = game:GetService("Players")

local part = script.Parent
local badgeService = game:GetService("BadgeService")
local badge = 2140926703

part.Touched:Connect(function(hit)
	local player = Players:GetPlayerFromCharacter(hit.Parent)

	if not player then
		return
	end

	local hasBadge = badgeService:UserHasBadgeAsync(player.UserId, badge)
	if hasBadge then
		print( player.Name .. " owns this badge!")
	else
		badgeService:AwardBadge(player.UserId, badge)
	end
end)

Script 3:

local badgeservice = game:GetService("BadgeService")
local id = 2140926703 -- Your badge ID here


-- Part touched
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)

I got these scripts from here. I don’t know why they aren’t giving me the badge. I checked and it doesn’t say I own it already so I don’t know what the problem is.
I tested this with a different part too to see if there was a problem with touching the part but it still didn’t work.
I checked the badge status and it appears to be fine and not content deleted or something so I don’t think that is the problem.
I’ve always had problems awarding a player a badge and I don’t know why. I can’t figure out the problem. It doesn’t seem to be an issue with the badge, scripts, or touching of the part. Maybe there is a problem that is specific to me. Example: “r6 people have a problem getting badges when they touch a part” or “because of this you cannot get badges you made yourself” or “you cannot get badges in roblox studio even as a test” but those are just examples that I thought of randomly.
Based off of what I have seen this is probably either a problem specific to me or a problem everyone can encounter but is just very unlikely a person encounters it, or I just haven’t gotten the right help.

Does it print any errors when the player touches the part?


I tested this script:

and it works fine.

Make sure the script’s Parent is the part you want players to touch in order to be awarded with the badge.

Make sure HTTP/ API is enabled as well!

It is the right part, and there have been errors with:

Script 1:
AwardBadge is not a valid member of BadgeService “BadgeService”

Script 2:
No errors

Script 3:
No errors

Edit: When I say “No errors” I mean that there are no visible errors. I still don’t get the badge.

Ensure API services are enabled in your game.

Still doesn’t work with API services enabled.

I am going to test the scripts in my test game and see if they work then. Maybe then I can figure out the issue.

It did not work in the test game. I came across a weird issue like this before. I wanted a part to wait for the player to touch it, then when the player touches the part it triggers another part to move. There is a 3rd part which waits for that part to collide with it and then it does something. Each part worked fine by itself but together it did not work.

This should work, good luck…

A normal script inside server script service,
while the part is called “CheckpointPart1”.

-- services
local badgeService = game:GetService("BadgeService")
local players = game:GetService("Players")

-- variable
local badge = 2140926703

--// would need to use datastore service to save values, and reload them
-- join event
players.PlayerAdded:Connect(function(player)
	local folder = Instance.new("Folder", player)
	folder.Name = "CheckpointsFolder"
	--
	local boolean1 = Instance.new("BoolValue", folder)
	boolean1.Name = "Checkpoint1"
	-- this boolean is not in use..
	local boolean2 = Instance.new("BoolValue", folder)
	boolean2.Name = "Checkpoint2"
end)

-- function
local function AwardBadge(player, badgeId)
	-- get badge information
	local success, badgeInfo = pcall(function()
		return badgeService:GetBadgeInfoAsync(badgeId)
	end)
	if success then
		-- can be awarded if badge is active..
		if badgeInfo.IsEnabled then
			-- award badge
			local awardSuccess, result = pcall(function()
				return badgeService:AwardBadge(player.UserId, badgeId)
			end)
		end
	else
		-- error
		warn("error fetching badge info: "..badgeInfo)
	end
end

--// make a duplicate of this code when making a new part
-- touch event
game:GetService("Workspace").CheckpointPart1.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local player = players:GetPlayerFromCharacter(hit.Parent)
		if player and not player.CheckpointsFolder.Checkpoint1.Value then -- use this if confused on the boolean: if player and player.CheckpointsFolder.Checkpoint1.Value == false then
			player.CheckpointsFolder.Checkpoint1.Value = true
			AwardBadge(player, badge)
		end
	end
end)

Don’t have the use the booleans and use could just check if the player already has the badge, do whatever you think would be best tbh…

local badgeService = game:GetService("BadgeService")
local badgeId = 2140926703

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

local player = game.Players:GetPlayerFromCharacter(hit.Parent)

	if hit.Parent:FindFirstChild("Humanoid")then
	
		badgeService:AwardBadge(player.UserId,id)
	end
end)

Should definitely work

I eventually solved the issue on a different topic, but thanks for the replies everybody.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.