Damage Not Occurring on Click

P.S. I’m a beginner scripter, so I am unexperienced

I’m currently working on a melee weapon. It has Debounce and Damage.
I’ve made the tool and script, but instead of damaging the humanoid when the tool is clicked, they take damage just by touching the tool. It’s like base damage instead of click/slash damage. Thank you for any help!

This is the script, it’s in the tool model.

local debounce = false

local function onTouched(hit)
	if debounce == false then
		debounce = true
		hit.Parent.Humanoid:TakeDamage(24) 
		wait(3)
		debounce = false
	end
end

script.Parent.Touched:Connect(onTouched)

This is surprisingly annoying to solve so I will explain it in steps as you get it working. You need another check to see if the sword is swinging. You turn this on and off when the sword starts and stops swinging. The player touched will then only take damage during this period.

So when the player clicks to swing to sword, turn this “damage enabled” value on for a second.

1 Like

How can i make the sword swing? Im not sure how to test the tool being activated

Would Tool.Activated:Connect(function() work?

1 Like

You can use the Tool.Activated event. Make sure RequiresHandle is set to false if you don’t have a Part named “Handle” in your Tool.

Yes that should work.

Search for “Linked Sword” in free models to find the old school “default sword”, it’s a decent example of how you can program a melee.

1 Like

Alright, ill check out the linked word when i get the chance to hop on my PC. Thank you for the help!

1 Like

NP, ask away if there’s something in there you’d like help with.

1 Like

here is no error in the code you provided. It looks like the debounce variable is being used as a debouncing mechanism to prevent the onTouched function from being called more than once every 3 seconds.

The debounce variable is initially set to false , and when the onTouched function is called, it checks the value of debounce . If it is false , the function runs and sets debounce to true , then waits 3 seconds before setting it back to false . This ensures that the onTouched function can only be called once every 3 seconds.

Yup, I just wanted to add something

I tried to implement the onActivated function, but it doesn’t seem to work. I think this could be because there is two Functions in a row, or a problem with the function itself. How can I fix this?

local debounce = false

local function onActivated()
local function onTouched(hit)
	if debounce == false then
		debounce = true
		hit.Parent.Humanoid:TakeDamage(24) 
		wait(3)
		debounce = false
	end
end

script.Parent.Touched:Connect(onTouched)
script.Parent.Parent.Activated:Connect(onActivated)


1 Like

Every function block has to be ended with an end. So a basic function that does nothing looks like this:

function doNothing()
end

You don’t have enough ends to close all the blocks, so you probably get an error in the output window.

1 Like

You could just check if a mouse is hovering over a player then get that player’s character and thus the humanoid allowing you to damage the player using Humanoid:TakeDamage(n).
As for not allowing the tool to damage players on touch you can just turn off the CanTouch property of the parts/meshparts.