Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

My Badge script isn't working to how I want it to, If you knows how to fix it I would be greatful?

Asked by 1 year ago
Edited 1 year ago

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)
0
show full script pls T3_MasterGamer 2189 — 1y
0
oh kk Puppy_lovertheawsome 84 — 1y
0
done Puppy_lovertheawsome 84 — 1y
0
oh also This is in the handle of my glove tool (This is an SB fan game) Puppy_lovertheawsome 84 — 1y
View all comments (7 more)
0
I am guessing if it is in the handle it might be a local script? I don't believe badges can be given out from the client to prevent exploits. Jay123abc2 241 — 1y
0
nah its not a local script its a normal script Puppy_lovertheawsome 84 — 1y
0
Rip I gtg now I will be back another time, Cya! Puppy_lovertheawsome 84 — 1y
0
Yeh, if it is on a local script that will be why. Badges can not be given from the client. If it is on a server script then it is a different issue. https://create.roblox.com/docs/reference/engine/classes/BadgeService#AwardBadge Jay123abc2 241 — 1y
0
ok thx be here another time cya Puppy_lovertheawsome 84 — 1y
0
Can't find the issue, check for any errors. Jay123abc2 241 — 1y
0
Can't find the issue, check for any errors. Jay123abc2 241 — 1y

1 answer

Log in to vote
2
Answered by 1 year ago
Edited 11 months ago
  1. game.Player doesn't exist and should be using game.Players.
  2. ePlr could be an NPC and that could break the script. You should always check if it's an actual player using Players:GetPlayerFromCharacter().
  3. Instead of indexing hit.Parent as a dictionary, you can use table.insert() and table.remove() as an array.
  4. Before awarding a badge, you should use BadgeService:GetBadgeInfoAsync() and BadgeService:UserHasBadgeAsync() to avoid errors.
  5. In case BadgeService:AwardService(), loop it until successful.
  6. When using LoadAnimation(), it is recommended to use Humanoid.Animator instead of Humanoid.
  7. Before playing animations, keep checking AnimationTrack.Length until it is more than zero (fully loaded).
  8. Tip: If you want an animation to play first than other animations, change AnimationTrack.Priority to Enum.AnimationPriority.Action4 (the highest priority).
  9. Disabling a script will make it stop and re-enabling it won't run again.
  10. Use 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)
1
tysm bro I thought all of my hard work was wasted! Your a life saver. Puppy_lovertheawsome 84 — 1y
Ad

Answer this question