Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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)

2 comments

0

The debounce should be at the first line after the event statement.

0

Now, it doesn't hits more than once, but this time, it hits even if I ain't pressing "Q".

1 answer

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)

2 comments

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?

0

I'm not able to help due to being out until Friday so you will probably fix it by then

This discussion is preserved as it was. New answers, comments, and votes are closed.