How can I make this a tool?

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)
5 Likes

You can use Unequipped event of the tool to detect when the tool is unequipped. You can use ContextActionService:UnbindAction() to make Punch action not work anymore after unequipping the tool. Similarly You can do it with Equipped and ContextActionService:BindAction().

3 Likes

This is my code now but it doesnt do anything and there are no errors

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

function onEquipped()
	cas:BindAction("Punch", punch, true, Enum.UserInputType.MouseButton1)
end

function Unequipped()
	cas:UnbindAction(punch)
end

l

1 Like

Yes, You made onEquipped() and Unequipped() functions but You didn’t connect these functions to the tool’s events.

like this?

function onEquipped(script.Parent)
local tool -- Define the tool path here
tool.Equipped:Connect(onEquipped)
2 Likes

its still not doing anything?

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

function onEquipped()
	cas:BindAction("Punch", punch, true, Enum.UserInputType.MouseButton1)
end

function Unequipped()
	cas:UnbindAction(punch)
end

script.Parent.Equipped:Connect(onEquipped)
script.Parent.Unequipped:Connect(Unequipped)
1 Like

Replace punch with "Punch" because You need to refer to the action name, not to the action’s function.

1 Like

still nothing is happening for some reason

Can you test it out in your own game to check?

Show the code again. Maybe there are more things wrong with the code.

full scripts:

local script (in tool)

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

function onEquipped()
	print("lol")
	cas:BindAction("Punch", punch, true, Enum.UserInputType.MouseButton1)
end

function Unequipped()
	cas:UnbindAction("Punch")
end

script.Parent.Equipped:Connect(onEquipped)
script.Parent.Unequipped:Connect(Unequipped)

server script in serverscriptservice:

local rs = game:GetService("ReplicatedStorage")

local events = rs:WaitForChild("Events")
local hitboxEvent = events:WaitForChild("Hitbox")

function newHitbox(character, size, offset, damage, linger)
	local hrp = character:FindFirstChild("HumanoidRootPart")
	if hrp == nil then return end
	local weld = Instance.new("WeldConstraint", hrp)
	local hitbox = Instance.new("Part")
	weld.Part0 = hrp
	weld.Part1 = hitbox

	hitbox.Transparency = 1
	hitbox.CanCollide = false
	hitbox.CanQuery = false
	hitbox.Massless = true

	hitbox.Size = Vector3.new(3,3,3)
	hitbox.CFrame = hrp.CFrame + hrp.CFrame.LookVector * offset.X + Vector3.new(0,offset.Y)
	hitbox.Parent = character

	hitbox.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") == nil then return end

		for _, v in pairs(hitbox:GetChildren()) do
			if v:IsA("ObjectValue") then
				if v.Value == hit.Parent then return end
			end
		end

		local hitCounter = Instance.new("ObjectValue", hitbox)
		hitCounter.Value = hit.Parent

		hit.Parent.Humanoid:TakeDamage(20)
	end)

	game.Debris:AddItem(hitbox, linger)
end

hitboxEvent.OnServerEvent:Connect(function(plr, size, offset, damage, linger)
	newHitbox(plr.Character, size, offset, damage, linger)
end)

if it doesnt work, maybe you can give an alternative script that works?

Does the output say anything? Try to add more printing commands and debug Your code like that.

there’s nothing in the output even after I added some more printing commands

Add print("I exist!") to the first line of the local script check the output.

 19:44:25.086  Boxing Legends @ 23 aug 2023 19:44 auto-recovery file was created  -  Studio
  19:44:39.639  I exist  -  Client - LocalScript:1

I will most likely come back to the problem tomorrow and we will continue thinking about this problem.
Edit: Show the current local code

1 Like
print("I exist")

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()
	print("punched")
	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

function onEquipped()
	print("equip")
	cas:BindAction("Punch", punch, true, Enum.UserInputType.MouseButton1)
end

function Unequipped()
	print("unequip")
	cas:UnbindAction("Punch")
end

script.Parent.Equipped:Connect(onEquipped)
script.Parent.Unequipped:Connect(Unequipped)

maybe some other people can help if they see this post

When you do script.Parent.Equ… does the word Equipped auto fill when you are typing it?