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

My punch script is damaging twice, thrice or maybe 10 times, why?

Asked by 6 years ago

I don't know why, but even if I am not pressing "Q", it can hit a humanoid when it's right hand or left hand is near the humanoid

local pressed = false
local trackanimation = nil
local playability = true
local hold = false
local value = script:GetChildren("PunchVal")
value = 1
function onKeyPress(inputObject, gameProcessedEvent)
    if inputObject.KeyCode == Enum.KeyCode.Q and not pressed then
        if playability == true and value == 1 then
            pressed = true
            local plr = game.Players.LocalPlayer
            trackanimation = plr.Character.Humanoid:LoadAnimation(animation1)
            trackanimation:Play()
            wait(0.7)
            pressed = false
            value = 2
            playability = false
            local Debounce = false
            local plr = game.Players.LocalPlayer
            plr.Character.RightHand.Touched:connect(function(hit)
            if hit.Name ~= "Skill" then
                        local humanoid = hit.Parent:findFirstChild("Humanoid")
                        if humanoid and humanoid ~= plr.Character.Humanoid then
                                if Debounce == false then
                                Debounce = true
                                wait(1)
                                script.Punch1Sound:Play()
                                humanoid:TakeDamage(1)
                                Debounce = false
                                if humanoid:TakeDamage(1) then wait(0.8)
                            end
                            end
                            end
                        end
            end)
0
The debounce should be at the first line after the event statement. Nep_Ryker 131 — 6y
0
Now, it doesn't hits more than once, but this time, it hits even if I ain't pressing "Q". Froredion 15 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago

I haven't been able to test this because I don't have the stuff needed in the if statements so hopefully this works!

local trackanimation = nil
local playability = true
local hold = false
local animation1 = nil --GIVE PATH TO ANIMATION HERE
local value = script:GetChildren("PunchVal")
local deb = true
value = 1
function onKeyPress(inputObject, gameProcessedEvent)
    if inputObject.KeyCode == Enum.KeyCode.Q and deb then
        if playability == true and value == 1 then
            deb = false
            local plr = game.Players.LocalPlayer
            trackanimation = plr.Character.Humanoid:LoadAnimation(animation1)
            trackanimation:Play()
            wait(0.7)
            value = 2
            playability = false
            local Debounce = false
            local plr = game.Players.LocalPlayer
            plr.Character.RightHand.Touched:connect(function(hit)
                if hit.Name ~= "Skill" then
                    local humanoid = hit.Parent:findFirstChild("Humanoid")
                    if humanoid and humanoid ~= plr.Character.Humanoid then
                        if Debounce == false then
                            Debounce = true
                            script.Punch1Sound:Play()
                            humanoid:TakeDamage(1)
                        end
                    end
                end
            end)
            wait(0.8)
            deb = true
        end
    end
end

game:GetService("UserInputService").InputBegan:connect(onKeyPress)
0
Not working, but I have an idea, if I can turn the value to 0 again, then it'd likely not get damage, but how? Froredion 15 — 6y
0
I'm not able to help due to being out until Friday so you will probably fix it by then AuthenticOakChair 68 — 6y
Ad

Answer this question