Just making my axe, anything was going well until something unexpected happens
What is the issue?
As you can see, I hit a dummy with my axe. And I saw something is not right. When I walk far from the dummy. The axe didn’t hit the dummy and still deal damage? Wait. What the heck?
What solutions have you tried so far?
I have no idea.
After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
local Melee = script.Parent
local Handle = Melee.Attacher
local HitPart = Melee.MeshPart
local IsKilling = false
local WhileKilling = false
-- Ignored variables --
local RandomAnims
local RandomHitSound
local SwingSound
local HitSound
local PlayHitSound
local PlayRandomAnims
local Idle
local Humanoid
-- Ignored variables --
local Settings = script.Parent:WaitForChild("Settings")
local CoolDown = Settings:WaitForChild("Cooldown")
local Second_CoolDown = Settings:WaitForChild("Second_Cooldown")
local WalkSpeed = Settings:WaitForChild("Speed")
local Damage = Settings:WaitForChild("Damage")
local function OnEquipping()
local Char = script.Parent.Parent
Humanoid = Char:WaitForChild("Humanoid")
Humanoid.WalkSpeed = WalkSpeed.Value
Idle = Humanoid:LoadAnimation(script.Idle_Anim.Idle)
Idle:Play()
RandomAnims = {
Humanoid:LoadAnimation(script.Swing_Anim.Swing1)
}
HitSound = {
Handle:WaitForChild("Hit")
}
end
local function OnAttack()
if not IsKilling then
IsKilling = true
PlayRandomAnims = RandomAnims[math.random(#RandomAnims)]
SwingSound = Handle:WaitForChild("Swing"):Play()
PlayRandomAnims:Play()
Idle:Stop()
HitPart.Touched:Connect(function(hit)
if IsKilling then
if hit.Parent:FindFirstChild("Humanoid") then
if not WhileKilling then
WhileKilling = true
PlayRandomAnims:GetMarkerReachedSignal("HitMarker"):Connect(function()
hit.Parent.Humanoid:TakeDamage(Damage.Value)
HitSound[math.random(#HitSound)]:Play()
end)
wait(Second_CoolDown.Value)
WhileKilling = false
end
end
elseif not IsKilling then
return
end
end)
wait(CoolDown.Value)
Idle:Play()
IsKilling = false
end
end
local function UnEquipping()
Humanoid.WalkSpeed = 16
IsKilling = false
WhileKilling = false
Idle:Stop()
end
Melee.Activated:Connect(OnAttack)
Melee.Equipped:Connect(OnEquipping)
Melee.Unequipped:Connect(UnEquipping)
It’s a little funny, but it’s 100% not your hitbox lol
You’re not cleaning up your connections. So every time you swing, you’re connecting another .Touched event. And with every .Touched event happening you’re connecting to GetMarkerReachedSignal.
So now each time you play that animation, it’ll do damage to that dummy. Even worse, each time your axe touches an instance, it’ll affect that other instance too.
To fix this, you have to disconnect your events like so:
local connection = HitPart.Touched:Connect(function(hit)
-- code
end)
task.wait(3) -- wait 3 seconds
connection:Disconnect() -- disconnects the connection so it wont run again
local Melee = script.Parent
local Handle = Melee.Attacher
local HitPart = Melee.MeshPart
local IsKilling = false
local WhileKilling = false
-- Ignored variables --
local RandomAnims
local RandomHitSound
local SwingSound
local HitSound
local PlayHitSound
local PlayRandomAnims
local Idle
local Humanoid
-- Ignored variables --
local Settings = script.Parent:WaitForChild("Settings")
local CoolDown = Settings:WaitForChild("Cooldown")
local Second_CoolDown = Settings:WaitForChild("Second_Cooldown")
local WalkSpeed = Settings:WaitForChild("Speed")
local Damage = Settings:WaitForChild("Damage")
local function OnEquipping()
local Char = script.Parent.Parent
Humanoid = Char:WaitForChild("Humanoid")
Humanoid.WalkSpeed = WalkSpeed.Value
Idle = Humanoid:LoadAnimation(script.Idle_Anim.Idle)
Idle:Play()
RandomAnims = {
Humanoid:LoadAnimation(script.Swing_Anim.Swing1)
}
HitSound = {
Handle:WaitForChild("Hit")
}
end
local function OnAttack()
if not IsKilling then
IsKilling = true
PlayRandomAnims = RandomAnims[math.random(#RandomAnims)]
SwingSound = Handle:WaitForChild("Swing"):Play()
PlayRandomAnims:Play()
Idle:Stop()
local connection = HitPart.Touched:Connect(function(hit)
if IsKilling then
if hit.Parent:FindFirstChild("Humanoid") then
if not WhileKilling then
WhileKilling = true
PlayRandomAnims:GetMarkerReachedSignal("HitMarker"):Connect(function()
hit.Parent.Humanoid:TakeDamage(Damage.Value)
HitSound[math.random(#HitSound)]:Play()
end)
wait(Second_CoolDown.Value)
WhileKilling = false
end
end
elseif not IsKilling then
return
end
end)
wait(CoolDown.Value)
connection:Disconnect()
Idle:Play()
IsKilling = false
end
end
local function UnEquipping()
Humanoid.WalkSpeed = 16
IsKilling = false
WhileKilling = false
Idle:Stop()
end
Melee.Activated:Connect(OnAttack)
Melee.Equipped:Connect(OnEquipping)
Melee.Unequipped:Connect(UnEquipping)