How can I make it so that hitting a player grants an achievement?

The system for my hitting uses invisible explosions, so I’m trying to make it so that when someone hits my character, it awards them with a badge.
Here’s my code.

function detect(thing)
	if thing.Parent.Name == "Engine" or thing.Name == "Bomb"  then
		local kaboom = Instance.new("Explosion")
		kaboom.BlastRadius = 10
		kaboom.Parent = thing
		kaboom.Position = thing.Position
		kaboom.BlastPressure = 1000
		kaboom.Visible = false
		thing.CanCollide=false
		thing.Transparency = 1
		local what = game.ServerStorage["explosion part"]:Clone()
		what.Position = thing.Position
		what.Parent = workspace
		what.ParticleEmitter:Emit(300)
		what.explode:Play()
		wait(2)
		what:Destroy()
		if thing.Parent:FindFirstChild("Humanoid") ~= nil then
			local player = game.Players:GetPlayerFromCharacter(thing.Parent)
			print(player.UserId)
			if player.UserId == 68496730 then


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

				local badgeID = 2125960837  -- Change this to your badge ID
				function hit(target)
					if target.Parent:FindFirstChild("Humanoid") ~= nil then
						local player = game.Players:GetPlayerFromCharacter(target.Parent)
						-- 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 == false then
							BadgeService:AwardBadge(player.UserId,badgeID)
							local confetti = game.ServerStorage.confettipart:Clone()
							confetti.Parent = workspace
							confetti.Position = target.Parent.Torso.Position
							confetti.ParticleEmitter:Emit(30)
							confetti["TF2 Achievement Earned"]:Play()
							-- Handle player's badge ownership as needed
						end
					end
				end
			end
		end
		script.Parent.Touched:Connect(hit)
	end
end

Any way to help?

use this code instead
Script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild(“Humanoid”) then
local plr = game.Players:GetPlayerFromCharacter(hit)
print(“Touched”)
local BadgeService = game:GetService(“BadgeService”)
local BadgeId = 1
BadgeService:AwardBadge(plr.UserId, BadgeId)
end
end)

I tried doing this but it didn’t work, but it also wasn’t the sort of idea I was going for.
Here is what it was.

did it errored? if yes then send ss

it didnt work
any other ideas on how to do this?

make sure to place script in startercharacterscripts

what about making it activate when the hammer hits?

oh wait I know why it won’t work

Script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild(“Humanoid”) then
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
print(“Touched”)
local BadgeService = game:GetService(“BadgeService”)
local BadgeId = 1
BadgeService:AwardBadge(plr.UserId, BadgeId)
end
end)

soooo, do i put it in the hammer head?
image_2022-04-16_130732570

if you want to put it in hammer then you should change it

Script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild(“Humanoid”) then
local player = script.Parent.Parent.Parent
local plr = game.Players:GetPlayerFromCharacter(player)
print(“Touched”)
local BadgeService = game:GetService(“BadgeService”)
local BadgeId = 1
BadgeService:AwardBadge(plr.UserId, BadgeId)
end
end)

here the hammer version try it

so it appears that it does not work, once again. :confused:

local BS = game:GetService("BadgeService")
local badgeid = 1 -- your badgeid
local owner = "YourName"
local debounce = false

script.Parent.Touched:Connect(function(hit)
if script.Parent.Parent:FindFirstChild("Backpack") then
local yourplayer = script.Parent.Parent.Parent
if hit.Parent:GetPlayerFromCharacter(hit.Parent) then
local otherplayer = GetPlayerFromCharacter(hit.Parent)
if otherplayer.Name == owner then
BadgeService:AwardBadge(yourplayer.UserId, BadgeId)
end
end
end
end)

where am i to parent this script?

You can put inside the Handle part.

testing now, will take a bit of time

script.Parent.Paret will be tool it can’t find backpack in it

But if you are parenting it in the handle you have to increase the parent by 1.

Example

if script.Parent.Parent.Parent:FindFirstChild(“Backpack”) then
local yourplayer = script.Parent.Parent.Parent.Parent
end)

Example

First parent is handle script.Parent
Second parent is tool script.Parent.Parent
Third parent is backpack script.Parent.Parent.Parent
fourth parent is player script.Parent.Parent.Parent.Parent

local BS = game:GetService("BadgeService")
local badgeid = 1 -- your badgeid
local owner = "YourName"
local debounce = false

script.Parent.Touched:Connect(function(hit)
if script.Parent.Parent.Parent:FindFirstChild("Backpack") then
local yourplayer = script.Parent.Parent.Parent.Parent
if hit.Parent:GetPlayerFromCharacter(hit.Parent) then
local otherplayer = GetPlayerFromCharacter(hit.Parent)
if otherplayer.Name == owner then
BadgeService:AwardBadge(yourplayer.UserId, BadgeId)
end
end
end
end)