I used a tutorial for a punching system but this is always active and not just when you equip the tool, so how can I make it so this only works when the ‘punch’ tool is equipped?
btw this is in a local script and there is also a script in serverscriptservice that registers the hitbox
local cas = game:GetService("ContextActionService")
local rs = game:GetService("ReplicatedStorage")
local events = rs:WaitForChild("Events")
local hitboxEvent = events:WaitForChild("Hitbox")
local plr = game.Players.LocalPlayer
local character = plr.Character or plr.CharacterAdded:Wait()
local hum = character:WaitForChild("Humanoid")
local animator = hum:WaitForChild("Animator")
local leftPunch = animator:LoadAnimation(script:WaitForChild("LeftPunch"))
local rightPunch = animator:LoadAnimation(script:WaitForChild("RightPunch"))
local currentPunch = 0
local lastPunch = 0
local debounce = false
local function punch()
if debounce then return end
if tick() - lastPunch > 5 then
currentPunch = 0
end
debounce = true
if currentPunch == 0 then
rightPunch:Play()
hitboxEvent:FireServer(Vector3.new(3,3,3), Vector3.new(2), 10, 0.3)
task.wait(0.6)
debounce = false
elseif currentPunch == 1 then
leftPunch:Play()
hitboxEvent:FireServer(Vector3.new(3,3,3), Vector3.new(2), 10, 0.3)
task.wait(0.6)
debounce = false
elseif currentPunch == 2 then
rightPunch:Play()
hitboxEvent:FireServer(Vector3.new(3,3,3), Vector3.new(2), 10, 0.3)
task.wait(0.8)
debounce = false
end
if currentPunch == 2 then
currentPunch = 0
else
currentPunch += 1
end
lastPunch = tick()
end
cas:BindAction("Punch", punch, true, Enum.UserInputType.MouseButton1)