I have a tool thats supposed to make the player able to punch, the issue with this is that after I activate the tool the debounce isnt doing anything. Once the tool is enabled the arms also dont stop doing damage even after the punch itself is done. The tool is only supposed to do one singular hit of 5 damage.
local player = game.Players.LocalPlayer
repeat wait() until player.Character
local char = player.Character
local hum = char:FindFirstChild("Humanoid") or char:WaitForChild("Humanoid")
local root = char:FindFirstChild("HumanoidRootPart")
local tool = script.Parent
local RS = game:GetService("ReplicatedStorage")
local Modules = RS:FindFirstChild("Modules") or RS:WaitForChild("Modules")
local PlayerHandler = require(Modules.PlayerHandler)
local anims = script.Parent.Animations
local punch1 = hum:LoadAnimation(anims.Punch1)
local punch2 = hum:LoadAnimation(anims.Punch2)
local punch_anims = {punch1, punch2}
local canTouch = true
local punching = false
tool.Equipped:Connect(function()
PlayerHandler:SetCrosshair(PlayerHandler.PlayerSettings.Crosshair)
end)
tool.Unequipped:Connect(function()
PlayerHandler:SetCrosshair()
end)
tool.Activated:Connect(function()
if canTouch and not punching then
local chosen_anim = punch_anims[math.random(1, #punch_anims)]
chosen_anim:Play()
canTouch = false
punching = true
wait(chosen_anim.Length)
punching = false
wait(0.2)
canTouch = true
end
local RightArm = player.Character:WaitForChild("Right Arm")
local LeftArm = player.Character:WaitForChild("Left Arm")
RightArm.Touched:connect(function(hit)
local model = hit:FindFirstAncestorOfClass("Model")
if model and model:FindFirstChild("Humanoid") then
model:FindFirstChild("Humanoid"):TakeDamage(5)
canTouch = false
wait()
canTouch = true
end
end)
LeftArm.Touched:connect(function(hit)
local model = hit:FindFirstAncestorOfClass("Model")
if model and model:FindFirstChild("Humanoid") then
model:FindFirstChild("Humanoid"):TakeDamage(5)
canTouch = false
wait()
canTouch = true
end
end)
end)
When tool.Activated is fired, the events you set inside(RightArm.Touched and LeftArm.Touched), keep listening for that event to fire.
So you may disconnect that events after a certain time: I.E
local player = game.Players.LocalPlayer
repeat wait() until player.Character
local char = player.Character
local hum = char:FindFirstChild("Humanoid") or char:WaitForChild("Humanoid")
local root = char:FindFirstChild("HumanoidRootPart")
local tool = script.Parent
local RS = game:GetService("ReplicatedStorage")
local Modules = RS:FindFirstChild("Modules") or RS:WaitForChild("Modules")
local PlayerHandler = require(Modules.PlayerHandler)
local anims = script.Parent.Animations
local punch1 = hum:LoadAnimation(anims.Punch1)
local punch2 = hum:LoadAnimation(anims.Punch2)
local punch_anims = {punch1, punch2}
local canTouch = true
local punching = false
tool.Equipped:Connect(function()
PlayerHandler:SetCrosshair(PlayerHandler.PlayerSettings.Crosshair)
end)
tool.Unequipped:Connect(function()
PlayerHandler:SetCrosshair()
end)
local connectionRA
local connectionLA
tool.Activated:Connect(function()
if canTouch and not punching then
local chosen_anim = punch_anims[math.random(1, #punch_anims)]
chosen_anim:Play()
canTouch = false
punching = true
wait(chosen_anim.Length)
punching = false
wait(0.2)
canTouch = true
end
local RightArm = player.Character:WaitForChild("Right Arm")
local LeftArm = player.Character:WaitForChild("Left Arm")
connectionRA = RightArm.Touched:connect(function(hit)
local model = hit:FindFirstAncestorOfClass("Model")
if model and model:FindFirstChild("Humanoid") then
model:FindFirstChild("Humanoid"):TakeDamage(5)
canTouch = false
wait()
canTouch = true
end
end)
connectionLA = LeftArm.Touched:connect(function(hit)
local model = hit:FindFirstAncestorOfClass("Model")
if model and model:FindFirstChild("Humanoid") then
model:FindFirstChild("Humanoid"):TakeDamage(5)
canTouch = false
wait()
canTouch = true
end
end)
task.wait(1); -- waits 1 second and then disconnects touched events
connectionRA:Disconnect()
connectionRA = nil
connectionLA:Disconnect()
connectionLA = nil
end)
Thanks, this fixes the arms problem, but while testing the script you sent I got this error 14:18:30.593 Players.Steamurs.Backpack.Punch.PunchClient:68: attempt to index nil with 'Disconnect' - Client - PunchClient:68
It’s because tool.Activated doesn’t have a debounce on it and you set the connectionRA to nil. So if it runs again before the first one is finished it’ll cause that error.
Instead of constantly creating and disconnecting touched events, you should probably set a variable that tells if the player is punching. You can actually just reusing the debounce variable, as it already tells you if the player is punching. Additionally, at the moment, you aren’t detecting punches immediately, because the code yields for the debounce before detecting damage.