I need help with this badge awarding script, can I please have help with it, I am following a series of tutorials by NetheriteStudio and im trying to fix some buggy script.
local Tool = script.Parent.Parent local Anim = script:WaitForChild("Animation") local AnimTrack local hitChars = {} local debounce = false Tool.Activated:Connect(function() if debounce then return end debounce = true local humanoid = Tool.Parent.Humanoid if not AnimTrack then AnimTrack = humanoid:LoadAnimation(Anim) end AnimTrack:Play() wait(0) debounce = false end) Tool.HitBox.Touched:Connect(function(hit) if hitChars[hit.Parent] or not debounce then return end if hit.Parent:FindFirstChild("Humanoid") then local eChar = hit.Parent local plrChar = Tool.Parent local eHumanRootPart = eChar:FindFirstChild("HumanoidRootPart") local plrHumanRootPart = plrChar:FindFirstChild("HumanoidRootPart") if plrHumanRootPart and eHumanRootPart then script.Disabled = true eChar.Humanoid.Sit = true local force = Instance.new("BodyVelocity", eHumanRootPart) force.MaxForce = Vector3.new(2,2,2) * math.huge local direction = (eHumanRootPart.CFrame.Position - plrHumanRootPart.CFrame.Position).Unit force.Velocity = (direction + Vector3.new(0,1,0)).Unit * 250 local rotation = Instance.new("BodyAngularVelocity", eHumanRootPart) rotation.AngularVelocity = Vector3.new(1, 1, 1) * math.pi * math.random(1,5) rotation.MaxTorque = Vector3.new(2,2,2) * math.huge rotation.P = 5000 wait(0.35) force:Destroy() rotation:Destroy() eChar.Humanoid.Sit = false local player = game.Players:GetPlayerFromCharacter(Tool.Parent) player.leaderstats.Slaps.Value = player.leaderstats.Slaps.Value + 1 local ePlr = game.Player:GetPlayerFromCharacter(eChar) local BS = game:GetService("BadgeService") local id = 2141407136 BS:AwardBadge(ePlr.UserId,id) end hitChars[hit.Parent] = true wait(2.5) script.Disabled = false hitChars[hit.Parent] = nil end end)
game.Player
doesn't exist and should be using game.Players
.ePlr
could be an NPC and that could break the script. You should always check if it's an actual player using Players:GetPlayerFromCharacter()
.hit.Parent
as a dictionary, you can use table.insert()
and table.remove()
as an array.BadgeService:GetBadgeInfoAsync()
and BadgeService:UserHasBadgeAsync()
to avoid errors.BadgeService:AwardService()
, loop it until successful.LoadAnimation()
, it is recommended to use Humanoid.Animator
instead of Humanoid
.AnimationTrack.Length
until it is more than zero (fully loaded).AnimationTrack.Priority
to Enum.AnimationPriority.Action4
(the highest priority).task.wait()
instead of wait()
.local Players = game:GetService("Players") local BadgeService = game:GetService("BadgeService") local Debris = game:GetService("Debris") local Tool = script:FindFirstAncestorOfClass("Tool") local Anim = script:WaitForChild("Animation") local hitChars = {} local debounce = false local animStopped = Instance.new("BindableEvent") Tool.Activated:Connect(function() if debounce then return end debounce = true local humanoid = Tool.Parent:FindFirstChildOfClass("Humanoid") local animator = humanoid:FindFirstChildOfClass("Animator") local AnimTrack = animator:LoadAnimation(Anim) repeat task.wait() until AnimTrack.Length -- loading AnimTrack.Priority = Enum.AnimationPriority.Action4 AnimTrack:Play() AnimTrack.Stopped:Connect(function() animStopped:Fire() end) AnimTrack.Ended:Connect(function() animStopped:Fire() end) animStopped.Event:Wait() -- waits until animation has stopped debounce = false end) local function badgeAvailable(badgeId: number, recipient: Player): boolean local suc1, suc2, info, hasBadge repeat suc1, info = pcall(BadgeService.GetBadgeInfoAsync, BadgeService, badgeId) until suc1 and info repeat suc2, hasBadge = pcall(BadgeService.UserHasBadgeAsync, BadgeService, recipient.UserId, badgeId) until suc2 if info.IsEnabled then if hasBadge then return true end end return false end Tool.HitBox.Touched:Connect(function(hit) if debounce then return end local eChar = hit:FindFirstAncestorOfClass("Model") local ePlr = Players:GetPlayerFromCharacter(eChar) local eHuman = eChar:FindFirstChildOfClass("Humanoid") local eHumanRootPart = eChar:FindFirstChild("HumanoidRootPart") local plrChar = Tool.Parent local plr = Players:GetPlayerFromCharacter(plrChar) local plrHuman = plrChar:FindFirstChildOfClass("Humanoid") local plrHumanRootPart = plrChar:FindFirstChild("HumanoidRootPart") if (eChar and ePlr and eHuman and eHumanRootPart) and (plrChar and plr and plrHuman and plrHumanRootPart) then -- if ePlr and plr are real players if table.find(hitChars, eChar) then return end eHuman.Sit = true local force = Instance.new("BodyVelocity", eHumanRootPart) force.MaxForce = Vector3.new(2,2,2) * math.huge local direction = (eHumanRootPart.CFrame.Position - plrHumanRootPart.CFrame.Position).Unit force.Velocity = (direction + Vector3.new(0,1,0)).Unit * 250 local rotation = Instance.new("BodyAngularVelocity", eHumanRootPart) rotation.AngularVelocity = Vector3.new(1, 1, 1) * math.pi * math.random(1,5) rotation.MaxTorque = Vector3.new(math.huge, math.huge, math.huge) rotation.P = 5000 Debris:AddItem(force, 0.35) Debris:AddItem(rotation, 0.35) task.wait(0.35) eHuman.Sit = false plr.leaderstats.Slaps.Value += 1 task.spawn(function() local badgeId = 2141407136 if badgeAvailable(badgeId, ePlr) then local success repeat success = pcall(BadgeService.AwardBadge, BadgeService, ePlr.UserId, id) until success or (not Players:FindFirstChild(ePlr.Name)) end end) table.insert(hitChars, eChar) task.wait(2.5) pcall(table.remove, hitChars, table.find(hitChars, eChar)) end end)