Help with random object spawning

I’m currently making a game and it requires you to find objects,
Currently, I am trying to make a script that will spawn the actual objects randomly on the map, now this does script does work, but for some reason, it will sometimes not spawn 5, and instead will spawn less (usually it will spawn 2 instead of 5). There are no errors or prints in console at all.

-- Constants
local shovel = game.ServerStorage.Tools.ShovelGround -- Change this to match the name of your shovel object
local mapSize = 100 -- Adjust this value according to your map size
local numberOfShovels = 5 -- Set the number of shovels to spawn
local maxAttempts = 100 -- Maximum attempts to find a suitable position

-- Function to generate random spawn position
local function getRandomPosition()
	local position = Vector3.new(
		math.random(-mapSize, mapSize), 
		5, -- Adjust this to control the height of the spawn
		math.random(-mapSize, mapSize)
	)
	return position
end

-- Function to check if the spawn location is clear
local function isClear(position)
	local region = Region3.new(position - Vector3.new(1, 1, 1), position + Vector3.new(1, 1, 1))
	local parts = workspace:FindPartsInRegion3(region, nil, math.huge)
	return #parts == 0
end

-- Function to spawn the shovel
local function spawnShovels()
	local shovelsSpawned = 0
	for i = 1, numberOfShovels do
		local position
		local attempts = 0
		repeat
			position = getRandomPosition()
			attempts = attempts + 1
		until isClear(position) or attempts >= maxAttempts

		if attempts < maxAttempts then
			local newShovel = shovel:Clone()
			newShovel.Parent = workspace
			newShovel:SetPrimaryPartCFrame(CFrame.new(position))
			shovelsSpawned = shovelsSpawned + 1
		else
			warn("Could not find a suitable position for shovel #" .. shovelsSpawned + 1)
		end
	end
end

-- Call the function to spawn shovels
spawnShovels()

(it’s 1:30 am and my brain is dead so don’t blame me if it’s something easy)

Is it possible that your isClear function is returning false 100 times in a row? Is that warn near the end being outputted?

It doesn’t seem like that, nothing is outputted in the console that has any connection with the script.

1 Like

Have you tried increasing the maxAttempts value?

Ok, have you tried debugging at all? Try adding a few prints in your spawnShovels function (ie in the attempts < maxAttempts branch of your if statement, printing attempts after the repeat loop), and see where that leads you.

Check in your explorer that the correct number of shovels is being spawned, wondering if they’re maybe outside the map or inside a wall somehow.

I have, yes, although I am thinking that the maxAttempts may be the issue, maybe I should try just removing that and make it loop infinitely until it finds a place, how much would that impact the performance do you think?

I don’t think it would affect performance but, I think i may have found the problem.

It’s probably that the 3 shovels just dont find a position to spawn in the 100 attempts.

That’s what I have been doing, only 2-3 have been spawned, the debugging part isn’t such a bad idea but I would like to see from the dev community to see if I have done something wrong that is an easy fix

Yes, that’s what I was thinking as well, I boosted it up to 100,000 attempts and it still doesn’t find anything, I’ll see what happens when I make it infinite.