So i tried to make a Sword/Tool but when i touched humanoid and go back and i start clicking and i touch the Humanoid again it kills the Humanoid when im not Clicking the tool. (Also im sorry for the bad Gramma english isnt my strongest). And dont blame if its a bad script im rlly new to this and this is also my first post. Heres the Code:
local Apple = script.Parent
local Handle = script.Parent.Handle
Apple.Activated:Connect(function()
Handle.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild(“Humanoid”) then
local char = hit.Parent
char.Humanoid:TakeDamage(10)
end
end)
end)
I don’t understand what you’re trying to do, are you trying to make it so you can only damage someone when you activate the tool? Everytime you click the tool, it just makes another Touched event which can cause a lot of memory usage, I think you can just have the Touched event by itself
Also I dont see a debounce in the Touched event, so it’s almost always going to instantly kill any humanoid it touches because the Touched event usually runs many times
local Apple = script.Parent
local Handle = script.Parent.Handle
local debounce = false
Apple.Activated:Connect(function()
Handle.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild(“Humanoid”) and not debounce then
debounce = true
local char = hit.Parent
char.Humanoid:TakeDamage(10)
wait(how long you want to wait)
debounce = false
end
end)
end)
you should use something like a debounce to delay each hit.
You have a touched connection inside of the activated, so whenever the tool clicks, it creates a connection in 10 clicks you’ll have 10 touched connections, I would recommend putting the touched connection outside the activated event, or at the very least disconnect it
if your putting the touched connection outside the activated event then use a variable to determine when the touched function should damage, that variable value would then be manipulated by your activated function
The issue is that you are creating a connection every time you click, which lasts even after the click is over.
This is bad since it will stack every time you click, eventually running out of memory.
Here’s a simple possible way you could do it instead.
local Apple = script.Parent
local Handle = script.Parent.Handle
local canHit = false
local canAttack = true
local cooldown = 1
Apple.Activated:Connect(function()
if not canAttack then return end
canAttack = false
canHit = true
wait(cooldown)
canHit = false
canAttack = true
end)
Handle.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild(“Humanoid”) and canHit then
local char = hit.Parent
char.Humanoid:TakeDamage(10)
end
end)
Note how cooldown can be changed to however long you want damage to happen after you click.
I have not tested the code so it may be wrong or have a typo. Let me know if it needs fixed.