Clone cloning more than requested

Hello ,

I ran into problem with cloning model .

local function CheckMob()
			
			for child = 1 , #mob do
				
				if child < 20 then
					
					local counter = newLoot:GetRandomLoot(1)
					
					local newmob = game.ReplicatedStorage.Mob:FindFirstChild(counter)
					
					if newmob then
						
						local clone = newmob:Clone()
						clone.Parent = game.Workspace.Map.Mob
						clone.PrimaryPart.CFrame = CFrame.new(char.HumanoidRootPart.CFrame * Vector3.new(10,10,10))
						print(counter)
						
					else
						return
					end
					
				else return end
				
		     end
		end

As you can see at the script , the limit is 20 but for some reasons the script didn’t stop and crashing the game experiences . I did some research but I can’t find any solution so far . Any help here is appreciated :slight_smile: :slight_smile: . Let me know if you need anything.

try using math.clamp(#mob,1,20)
and remove condition if child<20

still same or am I wrong . Here is the current script.

local function CheckMob()
			
			if  math.clamp(#mob , 1 , 20) then
				local counter = newLoot:GetRandomLoot(1)

				local newmob = game.ReplicatedStorage.Mob:FindFirstChild(counter)

				if newmob then

					local clone = newmob:Clone()
					clone.Parent = game.Workspace.Map.Mob
					clone.PrimaryPart.CFrame = CFrame.new(char.HumanoidRootPart.CFrame * Vector3.new(10,10,10))
					print(counter)

				end
			end
		end

CheckMob() bind on Event? If true you can use my module!

can you explain how sholud implement it . Im having hard time to understand it

You mean is module or else! I don’t know!
If is module you can read that topic!

I mean the module . Im trying to understand it but can you provide some explanations that a bit easier ??

Could you explain what you’re trying to achieve with your script?

since your code is pretty simple you just need to add this block of code and you’re done:

if on then return end
on= true
--code
on= false
local function CheckMob()
			if on then return end
on= true
			if  math.clamp(#mob , 1 , 20) then
				local counter = newLoot:GetRandomLoot(1)

				local newmob = game.ReplicatedStorage.Mob:FindFirstChild(counter)

				if newmob then

					local clone = newmob:Clone()
					clone.Parent = game.Workspace.Map.Mob
					clone.PrimaryPart.CFrame = CFrame.new(char.HumanoidRootPart.CFrame * Vector3.new(10,10,10))
					print(counter)

				end
			end
on= false
		end

I mean Im trying to loop all children[mob] inside folder and check if the children is less than 20 I want to clone new until it hit 20 . but for some reasons it clone infinitly

so I dont need to put this line ??

local newSystem:UISystem= UISystem.new('Test')
newSystem:Bind(workspace,'ChildAdded',function(child)
	print(child)
end)

local testSystem:UISystem= UISystemService:Find('Test')
--testSystem== newSystem

it is exam, and It for programer.
Your code is incomplete, I don’t even know exactly how CheckMob is called.CheckMob() or by Event .
Can you answer it?

just

CheckMob()

thats all

local function CheckMob()
	for i=1, 20- #fd:getChildren() do
		local counter = newLoot:GetRandomLoot(1)
					
		local newmob = game.ReplicatedStorage.Mob:FindFirstChild(counter)
		
		if newmob then
			
			local clone = newmob:Clone()
			clone.Parent = game.Workspace.Map.Mob
			clone.PrimaryPart.CFrame = CFrame.new(char.HumanoidRootPart.CFrame * Vector3.new(10,10,10))
			print(counter)
			
		else
			continue
		end
	end
end

fd is Folder contain mob!
I think here is what you want!

its still same . I did some changes but still same