Hello! Recently Ive made an attack for my game. However after I added damage indicators, I noticed the attacks hit more than once.
How could i fix this and make them only hit once?
Here is the script and a video of it in action:
game.ReplicatedStorage.MeteorCombo.OnServerEvent:Connect(function(player, cframe)
local Power = player.Stats.Power.Value
print(Power)
local character = player.Character
local rarm = character:FindFirstChild("Right Arm")
local rleg = character:FindFirstChild("Right Leg")
local touch
touch = rarm.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
if hit.Parent.Name ~= player.Name then
local character = hit.Parent
local humer = hit.Parent.Humanoid
humer.WalkSpeed = 0
character.Stun.Value = 1
humer.Health = humer.Health - (Power * (1))
if touch then touch:Disconnect()
end
character.Stun.Value = 0
wait(0.5)
--touch:Disconnect()
humer.WalkSpeed = 1
end
end
end)
wait(0.5)
local touch2
touch = rleg.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
if hit.Parent.Name ~= player.Name then
local character = hit.Parent
local humer = hit.Parent.Humanoid
humer.WalkSpeed = 0
character.Attacking.Value = 1
character.Stun.Value = 1
humer.Health = humer.Health - (Power * (1))
-- if touch ~= nil then touch:Disconnect()
--end
character.Stun.Value = 0
wait(2.5)
--touch:Disconnect()
humer.WalkSpeed = 16
end
end
end)
wait(1.2)
local touch3
touch = rarm.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
if hit.Parent.Name ~= player.Name then
local character = hit.Parent
local humer = hit.Parent.Humanoid
humer.WalkSpeed = 0
character.Attacking.Value = 1
character.Stun.Value = 1
humer.Health = humer.Health - (Power * (1))
-- if touch ~= nil then touch:Disconnect()
--end
character.Stun.Value = 0
wait(1)
-- touch:Disconnect()
humer.WalkSpeed = 16
end
end
end)
end)
When using .touched it’s literally checking if your part is hitting something every time it moves, so if for example :
arm hits a leg (counts as .touched)
arm is moving out of leg (counts as .touched)
I think your best option is to add a delay between when your attacks can do damage, for example
local debounce = false
part.touched:Connect(function())
if debounce == false then
debounce = true
(your damage code here)
task.wait(0.5)
debounce = false
end
end)
the delay could be the time it takes for your fighting animation to complete
I don’t added a lot of thing in but can you tell if is work:
i have just added “CanDamage”
game.ReplicatedStorage.MeteorCombo.OnServerEvent:Connect(function(player, cframe)
local Power = player.Stats.Power.Value
print(Power)
local character = player.Character
local rarm = character:FindFirstChild("Right Arm")
local rleg = character:FindFirstChild("Right Leg")
local canDamage = true -- i added this >>>>>>>>>>>>>>>>>>>>>>>>>
local touch
touch = rarm.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
if hit.Parent.Name ~= player.Name and canDamage == true then -- i added this >>>>>>>>>>>>>>>>>>>>>>>>>
local character = hit.Parent
local humer = hit.Parent.Humanoid
humer.WalkSpeed = 0
character.Stun.Value = 1
humer.Health = humer.Health - (Power * (1))
canDamage = false -- i added this >>>>>>>>>>>>>>>>>>>>>>>>>
if touch then touch:Disconnect()
end
character.Stun.Value = 0
wait(0.5)
--touch:Disconnect()
humer.WalkSpeed = 1
end
end
end)
wait(0.5)
local touch2
touch = rleg.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
if hit.Parent.Name ~= player.Name and canDamage == true then -- i added this >>>>>>>>>>>>>>>>>>>>>>>>>
local character = hit.Parent
local humer = hit.Parent.Humanoid
humer.WalkSpeed = 0
character.Attacking.Value = 1
character.Stun.Value = 1
humer.Health = humer.Health - (Power * (1))
canDamage = false -- i added this >>>>>>>>>>>>>>>>>>>>>>>>>
-- if touch ~= nil then touch:Disconnect()
--end
character.Stun.Value = 0
wait(2.5)
--touch:Disconnect()
humer.WalkSpeed = 16
end
end
end)
wait(1.2)
local touch3
touch = rarm.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
if hit.Parent.Name ~= player.Name and canDamage == true then -- i added this >>>>>>>>>>>>>>>>>>>>>>>>>
local character = hit.Parent
local humer = hit.Parent.Humanoid
humer.WalkSpeed = 0
character.Attacking.Value = 1
character.Stun.Value = 1
humer.Health = humer.Health - (Power * (1))
canDamage = false -- i added this >>>>>>>>>>>>>>>>>>>>>>>>>
-- if touch ~= nil then touch:Disconnect()
--end
character.Stun.Value = 0
wait(1)
-- touch:Disconnect()
humer.WalkSpeed = 16
end
end
end)
end)