Need help stopping item spawner script

I am not familiar with scripting.
I have an item spawner script that spawns the item I need however the spawner does not stop AND the items will spawn in the same spot multiple times. I need help stopping or disabling the script once the number of items I want to spawn has been reached and for those spawned items not to overlap.

This is the code I have so far that works flawlessly.

local spawns = workspace.ItemSpawns

local items = game.ReplicatedStorage.Items

local seconds = 3

local function chooseSpawn ()

local _spawn = spawns:GetChildren()[math.random(1,#spawns:GetChildren())]

return _spawn

end

local function chooseItem()

local item = items:GetChildren()[math.random(1,#items:GetChildren())]

return items

end

while wait (seconds) do

local _spawn = chooseSpawn()

local items = chooseItem()

local clone = items:Clone()

clone.Parent = workspace

clone.Corn.corng.CFrame = _spawn.CFrame * CFrame.new(0,5,0)

end

after adding the line ā€œif clone is >= 7ā€ the code stopped working entirely and giving the error, "ServerScriptService.SpawnerScript:20: Expected ā€˜thenā€™ when parsing if statement, got ā€˜breakā€™ "

local spawns = workspace.ItemSpawns
local items = game.ReplicatedStorage.Items
local clone = items:Clone()
local seconds = 3

local function chooseSpawn ()
	local _spawn = spawns:GetChildren()[math.random(1,#spawns:GetChildren())]
	
	return _spawn
end

local function chooseItem ()
	local item = items:GetChildren()[math.random(1,#items:GetChildren())]
	
	return items
end

while wait (seconds) do
	if clone is >= 7
		break
		
		else if clone < 7 do
			local _spawn = chooseSpawn()
			local items = chooseItem()

			clone.Parent = workspace
			clone.Corn.corng.CFrame = _spawn.CFrame * CFrame.new(0,5,0)	
			end
		end
	end
end

I would appreciate it if anyone could point me in the right direction of how to stop the spawner script and the items overlapping. Thank you.

1 Like

replace the line "if clone is >= 7 "
with
if clone >= 7 then

Try this.

local spawns = workspace.ItemSpawns
local items = game.ReplicatedStorage.Items
local clone = items:Clone()
local clonevalue = 0
local seconds = 3

local function chooseSpawn ()
	local _spawn = spawns:GetChildren()[math.random(1,#spawns:GetChildren())]

	return _spawn
end

local function chooseItem ()
	local item = items:GetChildren()[math.random(1,#items:GetChildren())]
	return items
        clonevalue = clonevalue + 1
end

while wait (seconds) do
	if clonevalue >= 7 then
	end
	if clonevalue < 7 then
		local _spawn = chooseSpawn()
		local items = chooseItem()

		clone.Parent = workspace
		clone.Corn.corng.CFrame = _spawn.CFrame * CFrame.new(0,5,0)	
end
end
1 Like
local spawns = workspace.ItemSpawns
local items = game.ReplicatedStorage.Items
local seconds = 3
local maxItems = 7

local function chooseSpawn ()
	local _spawn = spawns:GetChildren()[math.random(1,#spawns:GetChildren())]
	return _spawn
end

local function chooseItem ()
	local item = items:GetChildren()[math.random(1,#items:GetChildren())]:Clone()
	return item
end

for n = 1, maxItems do
	task.wait(seconds)
	local _spawn = chooseSpawn()
	local item = chooseItem()

	item.Corn.corng.CFrame = _spawn.CFrame * CFrame.new(0,5,0)
	item.Parent = workspace	
end

Sorry for the late reply, both your method and SelDrakenā€™s methods work exactly the same in that they spawn the item in a new spot each time however which i really weird. Any idea how to get it to spawn more then one?