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

Why my tool still making damage when it is unequipped?

Asked by
OnaKat 444 Moderation Voter
5 years ago
Edited 5 years ago

I want to make my own fighting style that it will do damage when tool is activate. After I test, it look like it work properly. But when i unequipped, it still do damage. Or equip but not attacking, it still do damage.

Here is my script

local tool = script.Parent
repeat wait() until tool.plr.Value ~= nil
repeat wait() until tool.plr.Value.Character ~= nil
local plr = tool.plr.Value
local char = plr.Character
local hum = char.Humanoid
local idle = hum:LoadAnimation(tool.Idle)

tool.Equipped:Connect(function()
    char.Animate.Disabled = true
    idle:Play()
end)
tool.Unequipped:Connect(function()
    char.Animate.Disabled = false
    idle:Stop()
end)
tool.Activated:Connect(function()
    if tool.Enabled == true and tool.Name == "Attack" then
        tool.Enabled = false
        tool.Name = "[ ... ]"
        local num = math.random(1,3)
        for _,attack in pairs(tool:GetChildren()) do
            if attack.Name == "Attack" then
                if attack.Style.Value == num then
                    local anim = hum:LoadAnimation(attack)
                    idle:Stop()
                    anim:Play()
                    if num == 1 then
                        char["Right Arm"].Touched:Connect(function(hit)
                            if hit.Parent:FindFirstChild("Humanoid") then
                                if hit.Parent ~= char then
                                    if hit.Parent.Humanoid:FindFirstChild(char.Name.."Attack"..num) then
                                    else
                                        hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - script.PunchDamage.Value
                                        local model = Instance.new("Model")
                                        model.Parent = hit.Parent.Humanoid
                                        model.Name = char.Name.."Attack"..num
                                        wait(0.5)
                                        model:Destroy()
                                    end
                                end
                            end
                        end)
                    elseif num == 2 then
                        char["Left Arm"].Touched:Connect(function(hit)
                            if hit.Parent:FindFirstChild("Humanoid") then
                                if hit.Parent ~= char then
                                    if hit.Parent.Humanoid:FindFirstChild(char.Name.."Attack"..num) then
                                    else
                                        hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - script.PunchDamage.Value
                                        local model = Instance.new("Model")
                                        model.Parent = hit.Parent.Humanoid
                                        model.Name = char.Name.."Attack"..num
                                        wait(0.5)
                                        model:Destroy()
                                    end
                                end
                            end
                        end)
                    elseif num == 3 then
                        char["Right Leg"].Touched:Connect(function(hit)
                            if hit.Parent:FindFirstChild("Humanoid") then
                                if hit.Parent ~= char then
                                    if hit.Parent.Humanoid:FindFirstChild(char.Name.."Attack1"..num) then
                                    else
                                        hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - script.KickDamage.Value
                                        local model = Instance.new("Model")
                                        model.Parent = hit.Parent.Humanoid
                                        model.Name = char.Name.."Attack1"..num
                                        wait(0.5)
                                        model:Destroy()
                                    end
                                end
                            end
                        end)
                        char["Left Leg"].Touched:Connect(function(hit)
                            if hit.Parent:FindFirstChild("Humanoid") then
                                if hit.Parent ~= char then
                                    if hit.Parent.Humanoid:FindFirstChild(char.Name.."Attack2"..num) then
                                    else
                                        hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - script.KickDamage.Value
                                        local model = Instance.new("Model")
                                        model.Parent = hit.Parent.Humanoid
                                        model.Name = char.Name.."Attack2"..num
                                        wait(0.5)
                                        model:Destroy()
                                    end
                                end
                            end
                        end)
                    end
                    anim.Stopped:Connect(function()
                        tool.Name = "Attack"
                        idle:Play()
                        tool.Enabled = true
                    end)
                end
            end
        end
    end
end)
0
You could simplify the script by making each Touched function calling a sub program. Tweakified 117 — 5y

1 answer

Log in to vote
1
Answered by
OnaKat 444 Moderation Voter
5 years ago

I have an idea!

Here is my new script!

local tool = script.Parent
repeat wait() until tool.plr.Value ~= nil
repeat wait() until tool.plr.Value.Character ~= nil
local plr = tool.plr.Value
local char = plr.Character
local hum = char.Humanoid
local idle = hum:LoadAnimation(tool.Idle)
local num = 0

tool.Equipped:Connect(function()
    char.Animate.Disabled = true
    idle:Play()
end)
tool.Unequipped:Connect(function()
    char.Animate.Disabled = false
    idle:Stop()
    script.KickDamage.Value = 0
    script.PunchDamage.Value = 0
end)
tool.Activated:Connect(function()
    if tool.Enabled == true and tool.Name == "Attack" then
        tool.Enabled = false
        tool.Name = "[ ... ]"
        script.KickDamage.Value = script.KickDamage.EditValueHere.Value
        script.PunchDamage.Value = script.PunchDamage.EditValueHere.Value
        num = math.random(1,3)
        for _,attack in pairs(tool:GetChildren()) do
            if attack.Name == "Attack" then
                if attack.Style.Value == num then
                    local anim = hum:LoadAnimation(attack)
                    idle:Stop()
                    anim:Play()
                    if num == 1 then
                        char["Right Arm"].Touched:Connect(function(hit)
                            if hit.Parent:FindFirstChild("Humanoid") then
                                if hit.Parent ~= char then
                                    if hit.Parent.Humanoid:FindFirstChild(char.Name.."Attack"..num) then
                                    else
                                        hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - script.PunchDamage.Value
                                        local model = Instance.new("Model")
                                        model.Parent = hit.Parent.Humanoid
                                        model.Name = char.Name.."Attack"..num
                                        wait(0.5)
                                        model:Destroy()
                                    end
                                end
                            end
                        end)
                    elseif num == 2 then
                        char["Left Arm"].Touched:Connect(function(hit)
                            if hit.Parent:FindFirstChild("Humanoid") then
                                if hit.Parent ~= char then
                                    if hit.Parent.Humanoid:FindFirstChild(char.Name.."Attack"..num) then
                                    else
                                        hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - script.PunchDamage.Value
                                        local model = Instance.new("Model")
                                        model.Parent = hit.Parent.Humanoid
                                        model.Name = char.Name.."Attack"..num
                                        wait(0.5)
                                        model:Destroy()
                                    end
                                end
                            end
                        end)
                    elseif num == 3 then
                        char["Right Leg"].Touched:Connect(function(hit)
                            if hit.Parent:FindFirstChild("Humanoid") then
                                if hit.Parent ~= char then
                                    if hit.Parent.Humanoid:FindFirstChild(char.Name.."Attack1"..num) then
                                    else
                                        hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - script.KickDamage.Value
                                        local model = Instance.new("Model")
                                        model.Parent = hit.Parent.Humanoid
                                        model.Name = char.Name.."Attack1"..num
                                        wait(0.5)
                                        model:Destroy()
                                    end
                                end
                            end
                        end)
                        char["Left Leg"].Touched:Connect(function(hit)
                            if hit.Parent:FindFirstChild("Humanoid") then
                                if hit.Parent ~= char then
                                    if hit.Parent.Humanoid:FindFirstChild(char.Name.."Attack2"..num) then
                                    else
                                        hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - script.KickDamage.Value
                                        local model = Instance.new("Model")
                                        model.Parent = hit.Parent.Humanoid
                                        model.Name = char.Name.."Attack2"..num
                                        wait(0.5)
                                        model:Destroy()
                                    end
                                end
                            end
                        end)
                    end
                    anim.Stopped:Connect(function()
                        tool.Name = "Attack"
                        idle:Play()
                        tool.Enabled = true
                    end)
                end
            end
        end
    end
end)
tool.Deactivated:Connect(function()
    if num == 1 or num == 2 then
        wait(0.25)
        print("Deactivated")
        script.KickDamage.Value = 0
        script.PunchDamage.Value = 0
        num = 0
    elseif num == 3 then
        wait(0.75)
        print("Deactivated")
        script.KickDamage.Value = 0
        script.PunchDamage.Value = 0
        num = 0
    end
end)

I add

tool.Deactivated:Connect(function()

and NumberValue in KickDamage/PunchDamage

I put script that

when tool is unequipped and deactivated it will change KickDamage and PunchDamage to 0

when tool is activated it will change KickDamage and PunchDamage to back to there damage

Ad

Answer this question