When I use my tool it continues dmg to whomever it touches. Uhh how do I fix this?
clientscript:
local UIS = game:GetService("UserInputService")
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid")
local CD = false
local remote = game.ReplicatedStorage.SwordDmg
local tool = script.Parent
local combo = 1
local slash = script.Parent.slash
tool.Activated:Connect(function()
local function dmg ()
tool.sward.Touched:Connect(function(hit)
local enemy = hit.Parent
local EnemyHum = enemy:FindFirstChild("Humanoid")
if not EnemyHum then return end
if CD == false then
remote:FireServer(EnemyHum, combo)
end
end)
end
local anim1 = humanoid:LoadAnimation(script:WaitForChild("anim1"))
local anim2 = humanoid:LoadAnimation(script:WaitForChild("anim2"))
local anim3 = humanoid:LoadAnimation(script:WaitForChild("anim3"))
if CD == false then
CD = true
if combo == 1 then
anim1:Play()
dmg()
combo = 2
slash:Play()
elseif combo == 2 then
anim2:Play()
dmg()
combo = 3
slash:Play()
elseif combo == 3 then
anim3:Play()
dmg()
combo = 1
slash:Play()
end
wait(0.3)
CD = false
end
end)
serverscript:
local remote = game.ReplicatedStorage.SwordDmg
remote.OnServerEvent:Connect(function(plr, EnemyHum, combo)
print("sent")
local PlrDB = {}
local hasBeenHit = false
for i,v in pairs(PlrDB) do -- go through all the players that have been hit, where 'v' is a member in the table/array/dictionary and 'i' is their number in the table. hopefully you already knew this
if v == EnemyHum.Parent then -- check if a player that has been hit is the player the part has touched
hasBeenHit = true -- disable
end
end
if hasBeenHit == false then
if combo == 1 then
EnemyHum.Parent.Humanoid:TakeDamage(5)
print(combo)
elseif combo == 2 then
EnemyHum.Parent.Humanoid:TakeDamage(7)
print(combo)
elseif combo == 3 then
print(combo)
EnemyHum.Parent.Humanoid:TakeDamage(10)
EnemyHum.Parent.HumanoidRootPart.Anchored = true
wait(0.7)
EnemyHum.Parent.HumanoidRootPart.Anchored = false
end
table.insert(PlrDB,EnemyHum.Parent)-- insert to table so it can't be hit again
end
end)

1 comment
Function on line 20 of Client script: The event is never disconnected meaning it is continuously listening for the Touched event to be fired. When the Touched event is fired, it runs the function(s) connected. This also leaves a larger issue the more the dmg() function is called (more connections are created than needed).