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

First punch doesn't do damage, but the others do?[SOLVED]

Asked by 4 years ago
Edited 4 years ago

Hi. I'm making a punch tool but for some reason the first punch just doesn't deal damage to the enemy NPC. I've been trying to fix this for a while now. How can I fix this? Thanks in advance. (Sorry for messy script)

Here's the punch tool script:

local tool = script.Parent
local hitsound = tool:WaitForChild("Hit")
local hit1sound = tool:WaitForChild("Hit2")
local swingsound = tool:WaitForChild("Swing")
local canattack = tool:WaitForChild("CanAttack")
local plr = tool.Parent.Parent
local debounce = false
local char = plr.Character or plr.CharacterAdded:wait()
local root = plr.Character:WaitForChild("HumanoidRootPart")
local char
local hum
local idle
local swing1anim
local swing2anim
local hit
local mouse
tool.Equipped:connect(function(mouse)
    swing1animation = swing1anim or script.Parent.Parent.Humanoid:LoadAnimation(script.Punch1)
    swing2animation = swing2anim or script.Parent.Parent.Humanoid:LoadAnimation(script.Punch2)
    char = char or plr.Character or plr.CharacterAdded:wait()
    hum = hum or char:WaitForChild("Humanoid")
    idle = idle or hum:LoadAnimation(script.Idle)
    idle:Play()
    idle.Looped = true
end)
tool.Activated:Connect(function(mouse)
    if debounce == false then
        wait(.001)
        local ray = Ray.new(root.CFrame.p, root.CFrame.lookVector*4.35)
        local hit, hitposition = workspace:FindPartOnRay(ray, char)
        local choose = math.random(1,2)
        if hit then
            wait(.001)
            local humanoid = hit.Parent:FindFirstChild("Humanoid") or hit.Parent:FindFirstChild("HumanoidE")
            if not humanoid ~= nil then
                humanoid = hit.Parent:FindFirstChild("Humanoid") or hit.Parent:FindFirstChild("HumanoidE")
            end
            if humanoid ~= nil then
                if choose == 1 then
                    debounce = true
                    swing1animation:Play()
                    swingsound:Play()
                    wait(.28)
                    hitsound:Play()
                    humanoid:TakeDamage(9)
                    wait(.33)
                    swingsound:Stop()
                    hitsound:Stop()
                    swing1animation:Stop()
                    debounce = false
                elseif choose == 2 then
                    debounce = true
                    swingsound:Play()
                    swing2animation:Play()
                    wait(.28)
                    hit1sound:Play()
                    humanoid:TakeDamage(9)
                    wait(.33)
                    swingsound:Stop()
                    hit1sound:Stop()
                    swing2animation:Stop()
                    debounce = false
                end
            else
                return
            end
        else
                if choose == 1 then
                    debounce = true
                    swingsound:Play()
                    swing1animation:Play()
                    wait(.7)
                    swingsound:Stop()
                    swing1animation:Stop()
                    debounce = false
                elseif choose == 2 then
                    debounce = true
                    swingsound:Play()
                    swing2animation:Play()
                    wait(.7)
                    swingsound:Stop()
                    swing2animation:Stop()
                    debounce = false
                end
            end
        end
end)
tool.Deactivated:connect(function()
    canattack.Value = true
end)
tool.Unequipped:Connect(function()
    idle:Stop()
end)

Fixed script:

local tool = script.Parent
local hitsound = tool:WaitForChild("Hit")
local hit1sound = tool:WaitForChild("Hit2")
local swingsound = tool:WaitForChild("Swing")
local canattack = tool:WaitForChild("CanAttack")
local plr = tool.Parent.Parent
local debounce = false
local char = plr.Character or plr.CharacterAdded:wait()
local root = plr.Character:WaitForChild("HumanoidRootPart")
local char
local hum
local idle
local swing1anim
local swing2anim
local hit
local scombatdamage
local humanoid
local ray
local hit
local hitposition
tool.Equipped:connect(function()
    scombatdamage = 9
    swing1animation = swing1anim or script.Parent.Parent.Humanoid:LoadAnimation(script.Punch1)
    swing2animation = swing2anim or script.Parent.Parent.Humanoid:LoadAnimation(script.Punch2)
    char = char or plr.Character or plr.CharacterAdded:wait()
    hum = hum or char:WaitForChild("Humanoid")
    idle = idle or hum:LoadAnimation(script.Idle)
    idle:Play()
    idle.Looped = true
    local ray = Ray.new(root.CFrame.p, root.CFrame.lookVector*4.35)
    local hit, hitposition = workspace:FindPartOnRay(ray, char)
    if hit then
        local humanoid = hit.Parent:FindFirstChild("Humanoid") or hit.Parent:FindFirstChild("HumanoidE")
        humanoid:TakeDamage(0)
    end
end)
tool.Activated:Connect(function()
    if debounce == false then
        ray = Ray.new(root.CFrame.p, root.CFrame.lookVector*4.35)
        hit, hitposition = workspace:FindPartOnRay(ray, char)
        local choose = math.random(1,2)
        if hit then
            humanoid = hit.Parent:FindFirstChild("Humanoid") or hit.Parent:FindFirstChild("HumanoidE")
            if not humanoid ~= nil then
                humanoid = hit.Parent:FindFirstChild("Humanoid") or hit.Parent:FindFirstChild("HumanoidE")
            end
            wait(.005)
            if humanoid ~= nil then
                if choose == 1 then
                    debounce = true
                    swing1animation:Play()
                    swingsound:Play()
                    wait(.28)
                    hitsound:Play()
                    humanoid:TakeDamage(scombatdamage)
                    wait(.33)
                    swingsound:Stop()
                    hitsound:Stop()
                    swing1animation:Stop()
                    debounce = false
                elseif choose == 2 then
                    debounce = true
                    swingsound:Play()
                    swing2animation:Play()
                    wait(.28)
                    hit1sound:Play()
                    humanoid:TakeDamage(scombatdamage)
                    wait(.33)
                    swingsound:Stop()
                    hit1sound:Stop()
                    swing2animation:Stop()
                    debounce = false
                end
            else
                return
            end
        else
                if choose == 1 then
                    debounce = true
                    swingsound:Play()
                    swing1animation:Play()
                    wait(.7)
                    swingsound:Stop()
                    swing1animation:Stop()
                    debounce = false
                elseif choose == 2 then
                    debounce = true
                    swingsound:Play()
                    swing2animation:Play()
                    wait(.7)
                    swingsound:Stop()
                    swing2animation:Stop()
                    debounce = false
                end
            end
        end
end)
tool.Deactivated:connect(function()
    canattack.Value = true
end)
tool.Unequipped:Connect(function()
    idle:Stop()
end)
0
indent your script... Its so confusing and its too long that nobody wants to read it Gingerely 245 — 4y

1 answer

Log in to vote
0
Answered by
JesseSong 3916 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago
local tool = script.Parent
local hitsound = tool:WaitForChild("Hit")
local hit1sound = tool:WaitForChild("Hit2")
local swingsound = tool:WaitForChild("Swing")
local canattack = tool:WaitForChild("CanAttack")
local plr = tool.Parent.Parent
local debounce = false
local char = plr.Character or plr.CharacterAdded:wait()
local root = plr.Character:WaitForChild("HumanoidRootPart")
local char
local hum
local idle
local swing1anim
local swing2anim
local hit
local mouse
tool.Equipped:connect(function(mouse)
    swing1animation = swing1anim or script.Parent.Parent.Humanoid:LoadAnimation(script.Punch1)
    swing2animation = swing2anim or script.Parent.Parent.Humanoid:LoadAnimation(script.Punch2)
    char = char or plr.Character or  plr.CharacterAdded:wait()
    idle = idle or hum:LoadAnimation(script.Idle)
    hum = hum or char:WaitForChild("Humanoid")

    humanoid:TakeDamage(9)

    hum:Play()
    idle.Looped = true
end)
tool.Activated:Connect(function(mouse)
    if debounce == false then
        wait(.001)
        local ray = Ray.new(root.CFrame.p, root.CFrame.lookVector*4.35)
        local hit, hitposition = workspace:FindPartOnRay(ray, char)
        local choose = math.random(1,2)
        if hit then
            wait(.001)
            local humanoid = hit.Parent:FindFirstChild("Humanoid") or hit.Parent:FindFirstChild("HumanoidE")
            if not humanoid ~= nil then
                humanoid = hit.Parent:FindFirstChild("Humanoid") or hit.Parent:FindFirstChild("HumanoidE")
            end
            if humanoid ~= nil then
                if choose == 1 then
                    debounce = true
                    swing1animation:Play()
                    swingsound:Play()
                    wait(.28)
                    hitsound:Play()
                    humanoid:TakeDamage(9)
                    wait(.33)
                    swingsound:Stop()
                    hitsound:Stop()
                    swing1animation:Stop()
                    debounce = false
                elseif choose == 2 then
                    debounce = true
                    swingsound:Play()
                    swing2animation:Play()
                    wait(.28)
                    hit1sound:Play()
                    humanoid:TakeDamage(9)
                    wait(.33)
                    swingsound:Stop()
                    hit1sound:Stop()
                    swing2animation:Stop()
                    debounce = false
                end
            else
                return
            end
        else
                if choose == 1 then
                    debounce = true
                    swingsound:Play()
                    swing1animation:Play()
                    wait(.7)
                    swingsound:Stop()
                    swing1animation:Stop()
                    debounce = false
                elseif choose == 2 then
                    debounce = true
                    swingsound:Play()
                    swing2animation:Play()
                    wait(.7)
                    swingsound:Stop()
                    swing2animation:Stop()
                    debounce = false
                end
            end
        end
end)
tool.Deactivated:connect(function()
    canattack.Value = true
end)
tool.Unequipped:Connect(function()
    idle:Stop()
end)
0
attempt to index nil with parent? SamuelaNutella 14 — 4y
0
l.23 SamuelaNutella 14 — 4y
0
I edited try this.. JesseSong 3916 — 4y
Ad

Answer this question