Cloning script cloning too many times

My cloning script would clone too many times. if anyone could help me, It would be highly appreciated!

local replicatedStorage = game:GetService("ReplicatedStorage")

local area = workspace.PotionSpawn
local areaSize = area.Size * 0.5

local min = area.Position - areaSize
local max = area.Position + areaSize

local part = replicatedStorage:WaitForChild("Potion")
--Some other code relating to the thing if needed

r = math.random(1,255)
g = math.random(1,255)
b = math.random(1,255)

partClone = part:Clone()
partClone.Handle.CFrame = CFrame.new(
	math.random(min.X, max.X), 
	min.Y + part.Handle.Size.Y * 0.5,
	math.random(min.Z, max.Z)
)
partClone.Handle.Potion.Color = Color3.fromRGB(r, g, b)
partClone.Parent = workspace.Potions
partClone.Name = 'Potion'

while wait() do
	for i,v in pairs(workspace.Potions:GetChildren()) do
		if v.Name == "Potion" then
			print(i)
			if i < 30 then
				r = math.random(1,255)
				g = math.random(1,255)
				b = math.random(1,255)

				partClone = part:Clone()
				partClone.Handle.CFrame = CFrame.new(
					math.random(min.X, max.X), 
					min.Y + part.Handle.Size.Y * 0.5,
					math.random(min.Z, max.Z)
				)
				partClone.Handle.Potion.Color = Color3.fromRGB(r, g, b)
				partClone.Parent = workspace.Potions
				partClone.Name = 'Potion'
			end
		end
	end
end

@volantias mate mate mate, have u check it again the codes?

Yeah, I have. Why? Is there something wrong with it?

1 Like

What do you mean by cloning too many times? How many times does it clone? How many times do you want it to clone?

Your main problem is that you are looping a clone loop.

while wait() do - this repeatedly will loop the clone loop creating infinite objects.

The problem could be that while you are iterating workspace.Potions, more items are getting put in that object, making it loop too much.
Have you tried putting the clone objects in somewhere else then parent them in workspace.Potions after?

@volantias mate it’s good for Double-checking it because u know the codes right or not
It does look ok!

First what is (i). Whatever i is you said
if i < 30 then
either you mean:
1-
if #i.Parent:GetChildren < 30 then
or 2-
if i:IsA("ValueBase") and i.Value < 30 then

hope you get what I mean! Welcomes!

NOTE: next time I think you should explain more.

There is an if statement, so it won’t.

1 Like