So I’m not entirely sure what to call this, but I have some code which for some reason it runs one more time each time it runs so the first time it runs it runs once, but then two times, three times, four times etc here’s a video of the output.
Heres the code
Whenever a move is inputted I call this function and all the first warns run once like “Move inputted”, “Player is attacking” and “Playing Animation” all run one time so the function is being called multiple times, but for the reason the connections end up running multiple times.
function Combat:MoveInputted(Move)
warn("Move inputted")
if not CharacterManager.IsAttacking then
warn("Player is attacking")
CharacterManager.IsAttacking = true
local ProxBlockArea
local Hitbox
local FD = Modules.Character.CurrentFacingDirection
warn("Playing Animation")
CharacterManager:StopAnimation()
CharacterManager:PlayAnimation(Move.Animation,"Combat")
CharacterManager.CurrentAnimation:GetMarkerReachedSignal("Active"):Connect(function()
warn("Hitbox Active")
if Move.HitboxData then
--| variables for hitboxes
local PBASize = Move.HitboxData.ProxBlockAreaSize
local PBAPos1 = Move.HitboxData.ProxBlockAreaPos[1]
local PBAPos2 = Move.HitboxData.ProxBlockAreaPos[2]
local HitboxSize = Move.HitboxData.HitboxSize
local HitboxPos1 = Move.HitboxData.HitboxPos[1]
local HitboxPos2 = Move.HitboxData.HitboxPos[2]
local Rotation = Move.HitboxData.Orientation
--| make 2 parts 1 for hitbox and one for the proxblockarea
ProxBlockArea = MakePart(PBASize,Main.HRP.Position+Vector3.new(PBAPos1*FD,PBAPos2,0),Rotation,"BlockRange",FD)
Hitbox = MakePart(HitboxSize,Main.HRP.Position+Vector3.new(HitboxPos1*FD,HitboxPos2,0),Rotation,"Hitbox",FD)
Connections["Hitbox"] = Hitbox.Touched:Connect(function(Hit)
HitboxFunc(Hitbox,Move)
end)
Connections["Hitbox"]:Disconnect()
Connections["ProxBlockArea"] = ProxBlockArea.Touched:Connect(function(Hit)
ProxBlockAreaFunc(ProxBlockArea)
end)
Connections["ProxBlockArea"]:Disconnect()
end
end)
--| The recovery marker indicates the animation ending and going back to an idle position
CharacterManager.CurrentAnimation:GetMarkerReachedSignal("Recovery"):Connect(function()
warn("Hitbox Not Active")
--| Destroy the the hitboxes and set the characters movement back to normal
CharacterManager:AdjustMovement(nil,CharacterManager.Jump,CharacterManager.Walk)
Hitbox:Destroy()
ProxBlockArea:Destroy()
for _,part in pairs(VisualHitboxs) do
part:Destroy()
end
end)
wait(0.5)
CharacterManager.IsAttacking = false
end
end
Here are the functions being used
local function MakePart(Size,Position,Rotation,Name,FD)
--| This part is the REAL hitbox that will make contact with the opponent
local Part = Instance.new("Part")
Part.CastShadow = false
Part.Name = Name
Part.Size = Size
Part.Position = Position --+ Vector3.new(0,0.5,3)
Part.CanCollide = false
Part.Anchored = false
Part.Color = Colors[Name]
Part.Material = Enum.Material.Neon
Part.Transparency = 1
if not Rotation then
Part.Orientation = Vector3.new(0,90,0)
else
Part.Orientation = Vector3.new(math.abs(Rotation[1])*FD,Rotation[2],Rotation[3])
end
local Weld = Instance.new("WeldConstraint")
Weld.Parent = Part
Weld.Part0 = Part
Weld.Part1 = Main.Character.HumanoidRootPart
Part.Parent = Main.Character
if ShowHitboxes.Value == true then
--| Fake hitbox which is a flat version of the real one for visualization
VisualHitbox = Instance.new("Part")
VisualHitbox.Name = "VisualHitbox"
VisualHitbox.Size = Vector3.new(0,Part.Size.Y,Part.Size.Z)
VisualHitbox.Orientation = Vector3.new(0,90,0)
VisualHitbox.CFrame = Part.CFrame + Vector3.new(0,0,1.3)
VisualHitbox.Color = Colors[Name]
VisualHitbox.Transparency = 0.5
local Weld2 = Instance.new("WeldConstraint")
Weld2.Parent = VisualHitbox
Weld2.Part0 = VisualHitbox
Weld2.Part1 = Main.Character.HumanoidRootPart
VisualHitbox.CanCollide = false
VisualHitbox.Parent = Main.Character
table.insert(VisualHitboxs,VisualHitbox)
end
--| Outline
local Outline = Instance.new("SelectionBox")
if ShowHitboxes.Value == true then
Outline.Transparency = 0
Outline.Adornee = VisualHitbox
Outline.Color3 = Colors[Name]
Outline.Parent = VisualHitbox
Outline.LineThickness = 0.03
end
return Part
end
local function ProxBlockAreaFunc(ProxBlockArea)
local TouchingParts = ProxBlockArea:GetTouchingParts()
for _,Part in pairs(TouchingParts) do
if Part.Parent ~= Main.Character and Part.Parent:FindFirstChild("HurtBox") then
Connections["ProxBlockArea"]:Disconnect()
local Target = Part.Parent
warn(Target.Name.." is within blocking range")
Main.Remotes.Blocking:FireServer(Target)
break
end
end
end
local function HitboxFunc(Hitbox,Move)
local TouchingParts = Hitbox:GetTouchingParts()
for _,Part in pairs(TouchingParts) do
if Part.Parent ~= Main.Character and Part.Parent:FindFirstChild("HurtBox") then
Connections["Hitbox"]:Disconnect()
local Target = Part.Parent
warn(Target.Name.." has been hit")
local TargetHurtboxSize = Target.HurtBox.Size
local TargetHurtboxPos = Target.HurtBox.Position
--| Hit Sound
local Sound = Instance.new("Sound")
Sound.SoundId = "rbxassetid://7148420114"
Sound.PlayOnRemove = true
Sound.Parent = workspace
Sound:Destroy()
--| Hit FX
local HitParticle = workspace.Model["Hit Particle"]:Clone()
HitParticle.CFrame = Target.HumanoidRootPart.CFrame
HitParticle.Parent = workspace.Game.FX
HitParticle.ParticleEmitter1:Emit(1)
HitParticle.ParticleEmitter2:Emit(1)
game.Debris:AddItem(HitParticle,0.3)
if not Target:FindFirstChild("IsBlocking") then
--> Reaction Animation
local Animator = Target.Humanoid.Animator
local Animation = Instance.new("Animation")
Animation.AnimationId = "rbxassetid://7168712174"
local Reaction = Animator:LoadAnimation(Animation)
Reaction:Play()
end
Main.Remotes.Damage:FireServer(Move.Name,Target,TargetHurtboxSize,TargetHurtboxPos)
break
end
end
end