Touched hitting more than once even with debounce

So I’m currently making a system that you can equip a weapon and hit with it along with animations, etc.
But there’s a problem, .Touched hits more than once, yeah I know and since I know it I added a debounce, after that, I did more things and finished it. However, the thing is that even with debounce somehow it’s hitting 2 times, it’s like it’s bypassing/ignoring the If statement along with the checkers and running the code.

I will be very grateful if anyone can help me fix it, I really need to finish this if I want to get this game published until the next month.

   local debounceequip = false
local equipped = false
local globalcooldown = false
local animdeb = false
local count = 1
local player = nil
local weapon = nil
local timeleft = 0.2
local punchcooldown = false
script.Parent.Something.OnServerEvent:Connect(function(plr)
	player = plr
	weapon = plr.Character:WaitForChild("Weapon",4)
end)

repeat wait() until weapon ~= nil

script.Parent.Equip.OnServerEvent:Connect(function(plr)
	if debounceequip == false and globalcooldown == false then
		spawn(function()
		local weapon = plr.Character:WaitForChild("Weapon")
		local anim = weapon:FindFirstChild("EquipAnimation")
		local loadanim = plr.Character:WaitForChild("Humanoid"):LoadAnimation(anim)
		globalcooldown = true
		if equipped == false then
			loadanim:Play()
			equipped = true
			debounceequip = true
			loadanim:Play()
			wait(0.1)
			weapon:FindFirstChild("Trail").Enabled = true
			weapon.Transparency = 0
		else
			equipped = false
			debounceequip = true
			weapon.Transparency = 1
		end
		loadanim.Stopped:connect(function()
			weapon:FindFirstChild("Trail").Enabled = false
			end)
		end)
		wait(1.5)
		debounceequip = false
		globalcooldown = false
	end
end)

script.Parent.Combat.OnServerEvent:Connect(function(plr)
	if animdeb == false and globalcooldown == false and equipped == true and punchcooldown == false then
		local finalpunch = false
		if count > 4 then
			count = 1
		elseif count == 4 then
		 finalpunch = true
		end
		punchcooldown = true
		local weapon = plr.Character:WaitForChild("Weapon")
		local anim = weapon:FindFirstChild("Slash"..count)
		local loadanim = plr.Character:WaitForChild("Humanoid"):LoadAnimation(anim)
		loadanim:Play()
		weapon.Slash:Play()
		weapon.Trail2.Enabled = true
		count = count + 1
		globalcooldown = true
		animdeb = true
		spawn(function()
			repeat 
				timeleft = timeleft - 0.01
				wait(0.01)
			until timeleft < 0.01
			timeleft = 0.2
		end)
		spawn(function()
		if finalpunch == false then
			wait(0.34)
			punchcooldown = false
		elseif finalpunch == true then
				wait(2)
				punchcooldown = false
			end
		end)
		wait(0.16)
		weapon.Trail2.Enabled = false
		animdeb = false
		wait(0.28)
		globalcooldown = false
	end
end)

weapon.Touched:connect(function(hit)
	if animdeb == true and hit.Parent:FindFirstChildOfClass("Humanoid") and hit.Parent ~= player.Character and hit.Parent:FindFirstChildOfClass("Humanoid").Health > 0 and hit.Parent:FindFirstChild(player.Name.."Hit") == nil and hit.Parent:FindFirstChild("BeingHit") == nil then
		if hit.Parent:FindFirstChild(player.Name.."Hit") and hit.Parent:FindFirstChild("BeingHit") then return end
		hit.Parent:FindFirstChild("HumanoidRootPart").CFrame = CFrame.new(hit.Parent:FindFirstChild("HumanoidRootPart").Position, player.Character:FindFirstChild("HumanoidRootPart").Position)
		local humanoid = hit.Parent:FindFirstChildOfClass("Humanoid")
		local thing = Instance.new("StringValue",hit.Parent)
		thing.Name = player.Name.."Hit"
		local thing2 = Instance.new("StringValue",hit.Parent)
		thing2.Name = "BeingHit"
		local damage = 4
		humanoid:TakeDamage(damage)
		local dmgindicator = game.ServerStorage.DamageIndicator:Clone()
		dmgindicator.Parent = hit.Parent:WaitForChild("Head",4)
		dmgindicator.TextLabel.Text = damage
		local anim = Instance.new("Animation",hit.Parent)
		anim.AnimationId = game.ServerStorage.Animations.LMB:FindFirstChild("Hit"..count).Value
		local e = humanoid:LoadAnimation(anim)
		e:Play()
		e.Stopped:Connect(function()
			game.Debris:AddItem(anim,0)
		end)
		if count == 5 then
		local bv = Instance.new("BodyVelocity",hit.Parent:FindFirstChild("HumanoidRootPart"))
		bv.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
		bv.Velocity = player.Character.HumanoidRootPart.CFrame.lookVector*40
		game.Debris:AddItem(bv,0.25)
		humanoid.PlatformStand = true
		spawn(function()
			wait(0.25)
			e:Stop()
			humanoid.PlatformStand = false
		end)
		else
		local bv = Instance.new("BodyVelocity",hit.Parent:FindFirstChild("HumanoidRootPart"))
		bv.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
		bv.Velocity = player.Character.HumanoidRootPart.CFrame.lookVector*2
		game.Debris:AddItem(bv,0.2)
		end
		game.Debris:AddItem(thing,timeleft)
		game.Debris:AddItem(thing2,timeleft)
		weapon.Hit:Play()
	end
end)

You want your debounce set to true outside the spawn. Try placing debounceequip = true before the spawn call and removing the two places inside the spawn.

For the animdeb I think you need to move the setting/unsetting closer to the checks. You don’t want a lot of code between when you check the debounce and when you set it

1 Like

The debounces are ok but for some reason, it’s hitting 2 or even more times. I don’t know how but I think it’s bypassing the checkers.

Functions can fire multiple times at once, the debounces aren’t working because there is a delay when you set the debounce to true in the spawn function.

1 Like

Got any idea of how can I fix it then? I will be very grateful

like I said earlier, you need to move the setting/unsetting up to right under the if check. So something like:

	if animdeb == false and globalcooldown == false and equipped == true and punchcooldown == false then
       animdeb = true
       globalcooldown = true
       punchcooldown = true
1 Like

Alright i will see what i can do

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.