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

The damaging tool activation function only ends after it hits,how do i fix that?

Asked by 2 years ago

I wanted to make a tool that if hits a player when activated then it will do damage,but for some reason when i activate the tool,miss and go up to a humanoid it still hits,how can i fix this? Really cant explain it more detailed in text,but heres a video https://youtu.be/ipaIv9afdRY The code:

local Tool = script.Parent.Parent
local Handle = script.Parent
local Idle = script.Idle
local Attack = script.Attack
local invFrames = false
local cooldown = false

Tool.Equipped:Connect(function()
    local humanoid = Tool.Parent:WaitForChild("Humanoid")
    local LoadIdle = humanoid:LoadAnimation(Idle)
    local weld = Instance.new("WeldConstraint")
    LoadIdle:Play()

    Tool.Activated:Connect(function()
        if cooldown == false then
            cooldown = true
            local humanoid = Tool.Parent:WaitForChild("Humanoid")
        local LoadAttack = humanoid:LoadAnimation(Attack)
            LoadIdle:Stop()
            weld.Part0 = Handle -- welds the tool to the right arm of the player holding it so it would look like the player is attacking with the tool
            weld.Part1 = humanoid.Parent["Right Arm"]
            weld.Parent = Handle
            LoadAttack:Play() -- plays the attack animation

            local touched = Handle.Touched:Connect(function(part)
                if part.Parent and part.Parent:FindFirstChild("Humanoid") and part.Parent.Name ~= humanoid.Parent.Name and invFrames ~= true then
                    part.Parent.Humanoid:TakeDamage(10)
                    invFrames = true
                end
            end)
            LoadIdle:Play()
            task.wait(5)
            cooldown = false
            invFrames = false


    else

        end

    end)
Tool.Unequipped:Connect(function()
        local humanoid = Tool.Parent:WaitForChild("Humanoid")
        local LoadAttack = humanoid:LoadAnimation(Attack)
        LoadIdle:Stop()
        LoadAttack:Stop()
    end)
end)

1 answer

Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

Is invFrames your boolen of telling the tool to attack or not?

If yes I think I manage to fix It, the reason why Its attacking after being swung Is because there was no code In the tool.Activated:Connect(function() part to end the invFrames, also I would recommend Instead of making the task.wait() number to less than 1 If Its a quick slash

I think this will fix It:

local Tool = script.Parent.Parent
local Handle = script.Parent
local Idle = script.Idle
local Attack = script.Attack
local invFrames = false
local cooldown = false

Tool.Equipped:Connect(function()
    local humanoid = Tool.Parent:WaitForChild("Humanoid")
    local LoadIdle = humanoid:LoadAnimation(Idle)
    local weld = Instance.new("WeldConstraint")
    LoadIdle:Play()

    Tool.Activated:Connect(function()
        if cooldown == false then
            cooldown = true
            invFrames = false
            local humanoid = Tool.Parent:WaitForChild("Humanoid")
            local LoadAttack = humanoid:LoadAnimation(Attack)
            LoadIdle:Stop()
            weld.Part0 = Handle -- welds the tool to the right arm of the player holding it so it would look like the player is attacking with the tool
            weld.Part1 = humanoid.Parent["Right Arm"]
            weld.Parent = Handle
            LoadAttack:Play() -- plays the attack animation
            wait(1)
            cooldown = false
            invFrames = false

            local touched = Handle.Touched:Connect(function(part)
                if part.Parent and part.Parent:FindFirstChild("Humanoid") and part.Parent.Name ~= humanoid.Parent.Name and invFrames ~= true then
                    part.Parent.Humanoid:TakeDamage(10)
                    invFrames = true
                end
            end)
            LoadIdle:Play()
            task.wait(5)
            cooldown = false
            invFrames = false


        else

        end

    end)
    Tool.Unequipped:Connect(function()
        local humanoid = Tool.Parent:WaitForChild("Humanoid")
        local LoadAttack = humanoid:LoadAnimation(Attack)
        LoadIdle:Stop()
        LoadAttack:Stop()
    end)
end)
1
it didnt fix it,but thank you for trying plato2002 3 — 2y
0
Its alright! imnotaguest1121 362 — 2y
Ad

Answer this question