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