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

Can someone please advise where I should place a debounce?

Asked by 8 years ago

Hey all,

I have a sword script that kills the opponent perfectly. However, there is a line in there that adds to the "Spree" stat. What happens is that, instead of one increment, it does multiple increments. I know that I need a debounce, but Where?

The entire script is around 170 lines long, but I have put down the place where it needs a debounce. Just to note: This was not the original script from where I got it from. If you want me to put in the original script, leave a comment.

Faulty Section

function blow(hit)
    local humanoid = hit.Parent:findFirstChild("Humanoid")
    local vCharacter = Tool.Parent
    local vPlayer = game.Players:playerFromCharacter(vCharacter)
    local hum = vCharacter:findFirstChild("Humanoid") -- non-nil if tool held by a character
    if humanoid~=nil and humanoid ~= hum and hum ~= nil then
        -- final check, make sure sword is in-hand

        local right_arm = vCharacter:FindFirstChild("Right Arm")
        if (right_arm ~= nil) then
            local joint = right_arm:FindFirstChild("RightGrip")
            if (joint ~= nil and (joint.Part0 == sword or joint.Part1 == sword)) then
                tagHumanoid(humanoid, vPlayer)
                humanoid:TakeDamage(damage)
                if humanoid.Health <= 0 and FindTag(humanoid,vPlayer) == true then
                    vPlayer.leaderstats.Spree.Value = vPlayer.leaderstats.Spree.Value +1
                end
                wait(1)
                untagHumanoid(humanoid)
            end
        end


    end
end

Full Script

r = game:service("RunService")


local damage = 5


local slash_damage = 8
local lunge_damage = 12

sword = script.Parent.Handle
Tool = script.Parent


local SlashSound = Instance.new("Sound")
SlashSound.SoundId = "rbxasset://sounds\\swordslash.wav"
SlashSound.Parent = sword
SlashSound.Volume = .7
SlashSound.Pitch = .9

local LungeSound = Instance.new("Sound")
LungeSound.SoundId = "rbxasset://sounds\\swordlunge.wav"
LungeSound.Parent = sword
LungeSound.Volume = .6
LungeSound.Pitch = .9

local UnsheathSound = Instance.new("Sound")
UnsheathSound.SoundId = "rbxasset://sounds\\unsheath.wav"
UnsheathSound.Parent = sword
UnsheathSound.Volume = 1


function blow(hit)
    local humanoid = hit.Parent:findFirstChild("Humanoid")
    local vCharacter = Tool.Parent
    local vPlayer = game.Players:playerFromCharacter(vCharacter)
    local hum = vCharacter:findFirstChild("Humanoid") -- non-nil if tool held by a character
    if humanoid~=nil and humanoid ~= hum and hum ~= nil then
        -- final check, make sure sword is in-hand

        local right_arm = vCharacter:FindFirstChild("Right Arm")
        if (right_arm ~= nil) then
            local joint = right_arm:FindFirstChild("RightGrip")
            if (joint ~= nil and (joint.Part0 == sword or joint.Part1 == sword)) then
                tagHumanoid(humanoid, vPlayer)
                humanoid:TakeDamage(damage)
                if humanoid.Health <= 0 and FindTag(humanoid,vPlayer) == true then
                    vPlayer.leaderstats.Spree.Value = vPlayer.leaderstats.Spree.Value +1
                end
                wait(1)
                untagHumanoid(humanoid)
            end
        end


    end
end

function FindTag(humanoid,player)
    children = humanoid:GetChildren()
    for i=1,#children do
        if children[i].Name == "creator" and children[i].Value == player then return true end
    end
end
function tagHumanoid(humanoid, player)
    local creator_tag = Instance.new("ObjectValue")
    creator_tag.Value = player
    creator_tag.Name = "creator"
    creator_tag.Parent = humanoid
end

function untagHumanoid(humanoid)
    if humanoid ~= nil then
        local tag = humanoid:findFirstChild("creator")
        if tag ~= nil then
            tag.Parent = nil
        end
    end
end


function attack()
    damage = slash_damage
    SlashSound:play()
    local anim = Instance.new("StringValue")
    anim.Name = "toolanim"
    anim.Value = "Slash"
    anim.Parent = Tool
end

function lunge()
    damage = lunge_damage

    LungeSound:play()

    local anim = Instance.new("StringValue")
    anim.Name = "toolanim"
    anim.Value = "Lunge"
    anim.Parent = Tool


    force = Instance.new("BodyVelocity")
    force.velocity = Vector3.new(0,10,0) --Tool.Parent.Torso.CFrame.lookVector * 80
    force.Parent = Tool.Parent.Torso
    wait(.2)
    swordOut()
    wait(.2)
    force.Parent = nil
    wait(.4)
    swordUp()

    damage = slash_damage
end

function swordUp()
    Tool.GripForward = Vector3.new(-1,0,0)
    Tool.GripRight = Vector3.new(0,1,0)
    Tool.GripUp = Vector3.new(0,0,1)
end

function swordOut()
    Tool.GripForward = Vector3.new(0,0,1)
    Tool.GripRight = Vector3.new(0,1,0)
    Tool.GripUp = Vector3.new(1,0,0)
end

function swordAcross()
    -- parry
end


Tool.Enabled = true
local last_attack = 0
function onActivated()

    if not Tool.Enabled then
        return
    end

    Tool.Enabled = false

    local character = Tool.Parent;
    local humanoid = character.Humanoid
    if humanoid == nil then
        print("Humanoid not found")
        return 
    end

    t = r.Stepped:wait()

    if (t - last_attack < .2) then
        lunge()
    else
        attack()
    end

    last_attack = t

    --wait(.5)

    Tool.Enabled = true
end


function onEquipped()
    UnsheathSound:play()
end


script.Parent.Activated:connect(onActivated)
script.Parent.Equipped:connect(onEquipped)


connection = sword.Touched:connect(blow)
0
Put the debounce if statement right at the start of the function, and set the debounce to true at the start, and false at the end. TheDeadlyPanther 2460 — 8y
0
@TheDeadlyPanther: Which function? fahmisack123 385 — 8y

1 answer

Log in to vote
0
Answered by
1waffle1 2908 Trusted Badge of Merit Moderation Voter Community Moderator
8 years ago

You don't want debounce. Instead of repeatedly telling the game you killed someone when they're already dead, make sure they're not already dead before you try to say you killed them. You can only kill someone once before they're dead.

It also isn't exactly necessary to check for the weld to the tool; that's really just to make sure you're alive. Here's what it should look like:

function blow(hit)
    local humanoid=hit.Parent:FindFirstChild("Humanoid")
    local vCharacter=Tool.Parent
    local vPlayer=game.Players:GetPlayerFromCharacter(vCharacter)
    local hum=vCharacter:FindFirstChild("Humanoid")
    if humanoid~=hum and humanoid and hum then
        if humanoid.Health>0 and hum.Health>0 then
            tagHumanoid(humanoid,vPlayer)
            humanoid:TakeDamage(damage)
            if humanoid.Health<=0 and FindTag(humanoid,vPlayer)then
                vPlayer.leaderstats.Spree.Value=vPlayer.leaderstats.Spree.Value+1
            end
            wait(1)
            untagHumanoid(humanoid)
        end
    end
end
0
So now if I kill him I SHOULD be rewarded once? fahmisack123 385 — 8y
0
will 1waffle1 2908 — 8y
Ad

Answer this question