I have a problem with the sword I created.
It uses .Touched event which basically fires everytime it touches a part/object (That also means that even if it hasn’t played an animation yet, It’ll still deal damage.)
The sword is also not a tool but an object that gets weld to the
player’s arm when equipped. (Similar to Swordburst 2)
What exactly do I need to do to make it so that It’ll only deal damage when clicked?
Example video: (Note that the sword model wasn’t created by me, I just used it to show you what happens)
Use a variable. If that variable is true, it can deal damage, if it’s false then it doesn’t deal damage. Then you just have to change that variable and set it to true when you want it to deal damage and false when you don’t want to. As easy as that.
local Tool = script.Parent
local Handle = Tool.Handle
local Damage = 0 --Change this.
local Connection
Tool.Activated:Connect(function()
Connection = Handle.Touched:Connect(function(Hit)
local HitModel = Hit:FindFirstAncestorOfClass("Model")
if HitModel then
local HitHuman = HitModel:FindFirstChildOfClass("Humanoid")
if HitHuman then
HitHuman:TakeDamage(Damage)
end
end
end)
end)
Tool.Deactivated:Connect(function()
if Connection then
if Connection.Connected then
Connection:Disconnect()
Connection = nil
end
end
end)
Here’s a general idea of what you should do, connect the handle’s “.Touched” event when the tool’s activated event fires and disconnect the handle’s “.Touched” event when the tool’s deactivated event fires.
Forummer’s reply is correct but a more basic way is:
local tool = script.Parent --We get our tool
local debounce = true --The specific thing you are asking for, we set up our debounce variable
tool.Touched:Connect(function() --Our function
local Enemy = tool.Parent:FindFirstChild("Humanoid") --We get the enemy's humanoid
if Enemy and debounce then
debounce = false --We set it to false again
Enemy.Health = Enemy.Health - 10 --We then damage the enemy
task.wait(5) --I used task.wait because it's more accurate
debounce = true --We set it back to true
end)