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

righthand.Touched function will not stop?

Everything works, except once the tool is activated, the right hand is constantly doing damage and doing knockback, any way to fix this? This is in localscript inside a tool

local player = game.Players.LocalPlayer
local plr = game.Players.LocalPlayer
mouse = player:GetMouse()
 
local hum = game.Players.LocalPlayer.Character:FindFirstChild("Humanoid")
local tool = script.Parent



debounce = false

tool = script.Parent --you hadnt set this to anything

hum = game:GetService("Players").LocalPlayer.Character.Humanoid -- you hadnt set this either

--local anims = {script.Animation1,script.Animation2}--stores the animations in an array
 
local anims = {
    script.Animation1;
    script.Animation1;
}




    -- the chosen attack was the first in the array anims
    -- that means that the animation chosen was script.Animation1



 -- gets a random number from 1 to 2
tool.Activated:Connect(function()

    if not debounce then   -- if debounce is false

        debounce = true --sets it to true

        local attack = math.random(2);

        local animation = hum:LoadAnimation(anims[attack]);
				
				animation:Play();

			if(attack == 1)then
				
				plr.Character.RightHand.Touched:connect(function(part)
			
			local rhOn = false
			
			if not rhOn then
				rhOn = true
		
			local D = plr.leaderstats.Strength
			local Dmg = D.Value / 4
			wait(0.5)
			if part.Parent:findFirstChild("Humanoid") then
				local hum = part.Parent.Humanoid
				
				hum:TakeDamage(Dmg)
				 
			end
			--Knockback
			local check = part.Parent:FindFirstChild("Humanoid")
				if check and part.Parent.Name ~= player.Name then
					local knock = Instance.new("BodyVelocity")
					check.Jump = true
					knock.Parent = part.Parent.Torso
					knock.maxForce = Vector3.new(math.huge,math.huge,math.huge)
					knock.velocity = player.Character.Head.CFrame.lookVector*90
					wait(0.2)
					knock:Destroy()
				end
				----
				
			wait(2)
			rhOn = false
			end
		end)
			end
        wait(2)--waits 1
        debounce = false--sets debounce to fals
    end
end)


1 answer

"Debouncing" is a fancy way of saying "check if we're already attacking before we attack". I don't know who coined the phrase, or where it came from. Debounce (read "wait before") the attack.

local attacking = false;

-- ... other code

player.Character.RightHand.Touched:connect(function(part)
	if(part.Parent:FindFirstChild("Humanoid"))then
		-- the touched part is part of a character, so we're now attacking the character
		attacking = true;
		-- ... other code handling the attack
		-- wait 1/2 a second before we can attack again
		wait(0.5);
		attacking = false;
	end
end)

-- ... other code

2 comments

0

what do i enter in the "attacking ="? true and false? or just leave it

0

the bolded values were taken out. thats what happened..

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