Hitbox Loops Parry Effect

  1. What do you want to achieve?

Hello, I want to create a Parry Game Function

  1. What is the issue?
    instead it loops and creates multiple parry effects when I just want one

  2. I’ve tried looking around on youtube, devfourm and even discord chats to find out what I can do to fix this. Honestly I just need tips and help on how to better the code? Heres what it looks like

External Media
function HitboxModule.NPCHitbox(NPCRoot, Weapon, Duration, Damage, Size)
	local Starttime = tick()
	local Connection
	local Weld = Instance.new("Weld")
	local Hitbox = Instance.new("Part")
	Hitbox.Size = Size
	Hitbox.BrickColor = BrickColor.new("Really red")
	Hitbox.Transparency = .4
	Hitbox.CanCollide = false
	Hitbox.CFrame = NPCRoot.CFrame * CFrame.new(0, 0, -3)
	Hitbox.Parent = Weapon
	Weld.Parent = Weapon
	Weld.Part0 = NPCRoot
	Weld.Part1 = Hitbox
	Weld.C1 = CFrame.new(0, 0, 3)
	local Parried = {}
	local FoundHumanoids = {}
	local Params = OverlapParams.new()
	Params.FilterDescendantsInstances = {NPCRoot.Parent}


	local function FindHumanoidCharacter()
		local FoundParts = workspace:GetPartsInPart(Hitbox, Params)

		for i, v in pairs(FoundParts) do
			if v:IsA("BasePart") and v.Parent:FindFirstChild("Humanoid") and not table.find(FoundHumanoids, v.Parent:FindFirstChild("Humanoid")) and v.Parent:GetAttribute("Blocking") == false then
				if v.Parent:GetAttribute("Parry") and not Parried[v.Parent:FindFirstChild("Humanoid")] then
					print("parried")

					local HitEffect = ReplicatedStorage.Effects.ParryEffect:Clone()
					HitEffect.Anchored = true
					HitEffect.Parent = workspace
					HitEffect.CFrame = v.Parent.HumanoidRootPart.CFrame * CFrame.new(0, 0, -2)
					for i, v in pairs(HitEffect.Attachment:GetChildren()) do
						print("looped")
						v:Emit(20)
						task.wait()
					end
					table.insert(Parried, v.Parent:FindFirstChild("Humanoid"))
					return 
				else
					if not FoundHumanoids[v.Parent:FindFirstChild("Humanoid")] then
						v.Parent:FindFirstChild("Humanoid"):TakeDamage(Damage)
						print("Damaged")
						table.insert(FoundHumanoids, v.Parent:FindFirstChild("Humanoid"))
					end
				end
				
			end
		end
	end
	Connection = RunService.Heartbeat:Connect(function()
		if tick() - Starttime >= Duration then
			table.clear(FoundHumanoids)
			Connection:Disconnect()
			Connection = nil
		else
			FindHumanoidCharacter()
		end
	end)
	Debris:AddItem(Hitbox, Duration)
end
1 Like