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

why does this function run more than once ?

Asked by 5 years ago
Edited 5 years ago

Hello, Full script is below thx already for any help

this script is for a sword so slash refers to the slashanimation, etc

my problem is that when i run the function DoDamage()[LINE 20] on line 49 or 63 the function never stops running, from that point if the sword just touches the dummy without actually doing the slash animation it already takes damage

how do i get the DoDamage() function to stop once it has hit the dummy?

if anything is unclear just comment thank you for the help

FULL SCRIPT:

script.Parent.Parent.Equipped:Connect(function()
    local char = script.Parent.Parent.Parent
    local hum = char.Humanoid

    local Slash1 = char.Humanoid:LoadAnimation(script.Parent.Slash1)
    local Slash2 = char.Humanoid:LoadAnimation(script.Parent.Slash2)
    idleanim = char.Animate.idle:WaitForChild('Animation1')
    toolnoneanim = char.Animate.toolnone:WaitForChild('ToolNoneAnim')
    local Stance = 'rbxassetid://2602051305'
    local Nothing = 'rbxassetid://2612295642'
    local DamagePoints = script.Parent.Parent.Damage.Value

    isHit = false
    damaging = false
    slashnum = 0

    idleanim.AnimationId = Stance
    toolnoneanim.AnimationId = Nothing

    function DoDamage() -- the function
        if damaging == true then
            script.Parent.Parent.MeshPart.Touched:Connect(function(hit)
                if not isHit then
                    isHit = true
                    print('Hit!')

                    local hithum = hit.Parent:FindFirstChild('Humanoid')
                    if hithum then
                        print('Found Humanoid')

                        hithum:TakeDamage(DamagePoints)
                        print(DamagePoints..' Damage dealt')
                    end
                    wait(0.5)
                    isHit = false
                    print(hithum.Health..'/'..hithum.MaxHealth..' '..hithum.Parent.Name..'!')
                end
            end)
        end
    end

    script.Parent.Parent.Activated:Connect(function()
        if not damaging then
            if slashnum == 0 then
                damaging = true

                Slash1:Play()
                print('1')
                DoDamage() -- starting the function
                wait(0.4)
                slashnum = 1

                damaging = false
            end
        end
    end)
    script.Parent.Parent.Activated:Connect(function()
        if not damaging then
            if slashnum == 1 then
                damaging = true

                Slash2:Play()
                DoDamage() -- starting the function
                wait(0.4)
                slashnum = 0

                damaging = false
            end
        end
    end)
end)
0
delete the wait after print(DamagePoints..' Damage dealt') end Gameplayer365247v2 1055 — 5y
0
that makes the dummy die faster when the sword is just touching it :/ TigerClaws454 48 — 5y

2 answers

Log in to vote
1
Answered by
ben0h555 417 Moderation Voter
5 years ago

You never disconnect the function from the .touched event, so it continues to call the function every time it is touched.

0
how would i disconnect the function from the .Touched ? TigerClaws454 48 — 5y
0
Figured it out thanks TigerClaws454 48 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

no you need to change the

wait(0.5)
isHit = false

to true because as long as its false the function will run or put

is hit = false after if hithum then

0
the isHit is set to true a few line before that that's the debounce TigerClaws454 48 — 5y
0
ye nvm Gameplayer365247v2 1055 — 5y

Answer this question