How to check how many parts have been cloned?

Hello, I’m making a hitbox system that creates (clones) a certain number of parts when the player swings a sword. These parts are the hitbox. They’re are cloned every 0.05 seconds and are positioned and oriented to the sword’s blade.

I’ve already coded all the cloned part’s (hitbox’s) properties. It is created in a while true do loop and I’m trying to figure out how to make the loop stop (stop the cloning) when a certain amount of clones have been created. (A remote event is fired to let the script know when to start and stop the cloning, which I’ve also already got set up). Just need it to stop the loop when a certain amount of clones have been created. Help is appreciated! Thanks.

P.S. I also want the clone’s CanTouch property to be turned on for a split instant then turned off (to do damage).

My LocalScript (located in StarterCharacterScripts) that creates the hitboxes when remote is fired:

local Hitbox1 = script.Parent["Right Arm"].Handle.Holder.Blade.Hitbox1
local Hitbox2 = script.Parent["Left Arm"].Handle2.Holder.Blade.Hitbox2

local Debris = game:GetService("Debris")

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local DamageStart = ReplicatedStorage.DamageStart
local DamageEnd= ReplicatedStorage.DamageEnd

local BreakLoop = false

function StartHitboxes()
	BreakLoop = false

	while true do
		
		task.wait(0.05)
		
		if BreakLoop == true then
			break
		end

		local Hitbox1Clone = Instance.new("Part", workspace)
		local Hitbox2Clone = Instance.new("Part", workspace)

		Hitbox1Clone.Name = "Hitbox"
		Hitbox2Clone.Name = "Hitbox"
		Hitbox1Clone.Size = Vector3.new(1.025, 1.46, 5.43)
		Hitbox2Clone.Size = Vector3.new(1.025, 1.46, 5.43)
		Hitbox1Clone.Anchored = true
		Hitbox2Clone.Anchored = true
		Hitbox1Clone.Transparency = 0.1
		Hitbox2Clone.Transparency = 0.1
		Hitbox1Clone.CanCollide = false
		Hitbox2Clone.CanCollide = false
		Hitbox1Clone.Material = Enum.Material.ForceField
		Hitbox2Clone.Material = Enum.Material.ForceField
		Hitbox1Clone.Color = Color3.new(1, 0, 0)
		Hitbox2Clone.Color = Color3.new(1, 0, 0)
		Hitbox1Clone.Massless = true
		Hitbox2Clone.Massless = true
		Hitbox1Clone.Position = Hitbox1.Position
		Hitbox2Clone.Position = Hitbox2.Position
		Hitbox1Clone.Orientation = Hitbox1.Orientation
		Hitbox2Clone.Orientation = Hitbox2.Orientation
		Hitbox1Clone.CanTouch = true
		Hitbox2Clone.CanTouch = true
		Hitbox1Clone.CanTouch = false
		Hitbox2Clone.CanTouch = false
		Debris:AddItem(Hitbox1Clone, 0.5)
		Debris:AddItem(Hitbox2Clone, 0.5)
	end
end

function StopHitboxes()
	BreakLoop = true
end

DamageStart.OnClientEvent:Connect(function()
	StartHitboxes()
end)

DamageEnd.OnClientEvent:Connect(function()
	StopHitboxes()
end)

The simplest thing you could do is have a variable that acts as a counter and update it for each clone. You can rely off of this counter and end the loop when a certain number was reached.

(Unless I am misreading this → Is the script that’s doing the cloning separate from the looped one?)

1 Like

How would I go about typing this out in my script? (scripting isn’t my strongest when it comes to development.)

1 Like

inside the StartHitboxes function, make a variable and set it to 0, after each iteration, increase its value by 1, and in the while loop, check if the value is smaller than the max. amount of clones, if it is, then just break

or alternatively, use a for loop.

2 Likes

thx for replying! I kinda need help on how to type that out. I got stuck on how to make it +1 every time it loops. but for the variable I typed Local HitboxCount = 0

1 Like
function StartHitboxes()
	BreakLoop = false
	local counter = 0

	while true do
		
		task.wait(0.05)
		
		if BreakLoop == true or counter >= CERTAIN_AMOUNT_OF_CLONES then
			break
		end

		local Hitbox1Clone = Instance.new("Part", workspace)
		local Hitbox2Clone = Instance.new("Part", workspace)

		Hitbox1Clone.Name = "Hitbox"
		Hitbox2Clone.Name = "Hitbox"
		Hitbox1Clone.Size = Vector3.new(1.025, 1.46, 5.43)
		Hitbox2Clone.Size = Vector3.new(1.025, 1.46, 5.43)
		Hitbox1Clone.Anchored = true
		Hitbox2Clone.Anchored = true
		Hitbox1Clone.Transparency = 0.1
		Hitbox2Clone.Transparency = 0.1
		Hitbox1Clone.CanCollide = false
		Hitbox2Clone.CanCollide = false
		Hitbox1Clone.Material = Enum.Material.ForceField
		Hitbox2Clone.Material = Enum.Material.ForceField
		Hitbox1Clone.Color = Color3.new(1, 0, 0)
		Hitbox2Clone.Color = Color3.new(1, 0, 0)
		Hitbox1Clone.Massless = true
		Hitbox2Clone.Massless = true
		Hitbox1Clone.Position = Hitbox1.Position
		Hitbox2Clone.Position = Hitbox2.Position
		Hitbox1Clone.Orientation = Hitbox1.Orientation
		Hitbox2Clone.Orientation = Hitbox2.Orientation
		Hitbox1Clone.CanTouch = true
		Hitbox2Clone.CanTouch = true
		Hitbox1Clone.CanTouch = false
		Hitbox2Clone.CanTouch = false
		Debris:AddItem(Hitbox1Clone, 0.5)
		Debris:AddItem(Hitbox2Clone, 0.5)
		counter += 1
	end
end

Obviously you have to change CERTAIN_AMOUNT_OF_CLONES to the amount of clones you want.
Honestly i would do it with a for loop if i was you, at the end of the day its ur choice tho.
Also, you don’t have to change each property of the Hitboxes one by one, you could just do it for one of them, clone it, and change the values that are different for both.

2 Likes

Also when I keep swinging the swords, it does create 20 hitboxes still like it’s suppose to, but it’s like creating them at weird times…