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.
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.
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)
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.