Whenever I do an attack with my sword, even if I do not attack again it’ll still do damage if it clips through a player, is there any way to make it do damage only one time? My code consists of a local script that just fires a remote event and a script that looks for the remote event and then does damage to the thing it hits, but it keeps doing damage after the attack has ended.
I’ve tried to make a bool that sets a variable to true so that it only does damage once, but then I cannot find a way to turn that variable back to false in order to deal damage again.
local tool = script.Parent
tool.LeftSwing.OnServerEvent:Connect(function()
tool.Hitbox.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
hit.Parent.Humanoid:TakeDamage(25)
end
end)
end)
tool.RightSwing.OnServerEvent:Connect(function()
tool.Hitbox.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
hit.Parent.Humanoid:TakeDamage(25)
end
end)
end)
local tool = script.Parent
local damageDebounce = false
function Damage(hit, connection)
if hit.Parent:FindFirstChild("Humanoid") then
if not damageDebounce then
damageDebounce = true
hit.Parent.Humanoid:TakeDamage(25)
pcall(function()
connection:Disconnect()
connection = nil
end)
delay(2.5, function()
damageDebounce = false
end)
end
end
end
tool.LeftSwing.OnServerEvent:Connect(function()
local Connection
Connection = tool.Hitbox.Touched:Connect(function(Hit)
Damage(Hit, Connection)
end)
end)
tool.RightSwing.OnServerEvent:Connect(function()
local Connection
Connection = tool.Hitbox.Touched:Connect(function(Hit)
Damage(Hit, Connection)
end)
end)
This happens because the Touched event fires whenever the object is touching a part. If you only want the player to do damage after they stop hitting the other player, you can use TouchEnded (not the best event) to set the debounce to false which signals that they stopped touching the player.
I also changed it so it disconnects the Touched event
local tool = script.Parent
local damageDebounce = false
function Damage(hit, connection)
if hit.Parent:FindFirstChild("Humanoid") then
if not damageDebounce then
damageDebounce = true
hit.Parent.Humanoid:TakeDamage(25)
pcall(function()
connection:Disconnect()
connection = nil
end)
delay(2.5, function()
damageDebounce = false
end)
end
end
end
tool.LeftSwing.OnServerEvent:Connect(function()
local Connection
Connection = tool.Hitbox.Touched:Connect(function(Hit)
Damage(Hit, Connection)
end)
end)
tool.RightSwing.OnServerEvent:Connect(function()
local Connection
Connection = tool.Hitbox.Touched:Connect(function(Hit)
Damage(Hit, Connection)
end)
end)
The problem is because you dont disconnect the event, try disconnecting it.
In case you dont know how to disconnect…
local connection
connection = mouse.Button1Down:Connect(function()
connection:Disconnect()
end)
-- OR
local connection
connection = mouse.Button1Down:Connect(function()
end)
connection:Disconnect() -- This would disconnect right after, just placed this so you know that you can disconnect outside the event too
Used button1down as a example but you can use anything else
Disconnecting the Button1Down/Button1Up will still damage the player/npc because the touched event is being used on the server.
Use this code instead:
local tool = script.Parent
local damageDebounce = false
function Damage(hit, connection)
if hit.Parent:FindFirstChild("Humanoid") then
if not damageDebounce then
damageDebounce = true
hit.Parent.Humanoid:TakeDamage(25)
pcall(function()
connection:Disconnect()
connection = nil
end)
delay(2.5, function()
damageDebounce = false
end)
end
end
end
tool.LeftSwing.OnServerEvent:Connect(function()
local Connection
Connection = tool.Hitbox.Touched:Connect(function(Hit)
Damage(Hit, Connection)
end)
end)
tool.RightSwing.OnServerEvent:Connect(function()
local Connection
Connection = tool.Hitbox.Touched:Connect(function(Hit)
Damage(Hit, Connection)
end)
end)
local tool = script.Parent
local damageDebounce = false
function Damage(hit, connection)
if hit.Parent:FindFirstChild("Humanoid") then
if not damageDebounce then
damageDebounce = true
hit.Parent.Humanoid:TakeDamage(25)
pcall(function()
connection:Disconnect()
connection = nil
end)
delay(2.5, function()
damageDebounce = false
end)
end
end
end
tool.LeftSwing.OnServerEvent:Connect(function()
local Connection
Connection = tool.Hitbox.Touched:Connect(function(Hit)
Damage(Hit, Connection)
Connection = nil
end)
end)
tool.RightSwing.OnServerEvent:Connect(function()
local Connection
Connection = tool.Hitbox.Touched:Connect(function(Hit)
Damage(Hit, Connection)
Connection = nil
end)
end)