Help with chest spawning

It wont randomly spawn but spawn in the same spot. I am trying to make it so that 3 chest spawn in the map randomly and not be in the same spot.

Script
local Map = game.Workspace.Maps:FindFirstChild(ChosenMap.Name)
	local Chest_Spawns = Map.Chest_Spawns:GetChildren()
	for i = 1,3 do
		local Picked_Spawn = Chest_Spawns[math.random(1,#Chest_Spawns)]
		local Chest = game.ServerStorage.Items.Chest:Clone()
		Chest.Parent = game.Workspace.ChestFolder
		for _, Chests in pairs(game.Workspace.ChestFolder:GetChildren()) do
			if Chests:IsA("Model") and Chests.Name == "Chest" then
				if Chests.Chest_Spawn.Position == Picked_Spawn.Position then
					local NewPickedSpawn = Chest_Spawns[math.random(1,#Chest_Spawns)]
					repeat task.wait(0.1) 
						NewPickedSpawn = Chest_Spawns[math.random(1,#Chest_Spawns)]
					until Picked_Spawn.Position ~= NewPickedSpawn.Position
					Chest.Chest_Spawn.Position =NewPickedSpawn.Position
					Chest.Chest_Spawn.Orientation = NewPickedSpawn.Orientation
				else
					Chest.Chest_Spawn.Position = Picked_Spawn.Position
					Chest.Chest_Spawn.Orientation = Picked_Spawn.Orientation
				end
			end
		end
	end

you mean the three chests spawn at the same position? Or every time you call this script the three chests clone to the same position?

1 Like

Yes they spawn in the same position and they should spawn in different spawn locations.

Gib me a sec, I’l try to work it out

1 Like

Simply have an array in which you insert spawns as selected in the for loop. Then check that array using table.find to see if there’s a match. If not, use that location and add to the array. If there is a match, try another random location until there isn’t a match.

1 Like

The thing is its all going in 1 spot its already made to go in a random position. I also made a if statement seeing if the chest spawn position is equal to the chest position and if it is it chooses a new one

Change
if Chests:IsA("Model")
to
if Chests:IsA("BasePart")
and tell me if it works or not

1 Like

The chest is a model, it wouldnt work if it was a basepart. I’m pretty sure its the for i = 1,3 do

It also seems that you’re repeating a check anyway. First with the original if statement, then again with a loop. You shouldn’t use a loop in a situation like this. Instead, you should use a recursive function.

1 Like

try printing something after the “If chest is a model” and see if it prints

1 Like

I’m telling you that’s not it causes there’s no error.

Wait does using return rerun the script?

No, can you try what I said and tell me if it prints something

it printed that the chest was a model

return will return any value given. Here’s an example:

function Add(a,b)
	return a + b
end

Here’s a weird example of a recursive function to count from a given number to 10:

function CountUntil(number,startingNumber,current)
	startingNumber = startingNumber or 0
	current = current or startingNumber
	current += 1
	if current >= number then
		return true
	else
		return CountUntil(number,startingNumber,current)
	end
end
CountUntil(10)
1 Like

so in my case how would I use it? i found the problem was that I was setting the position of chest_spawn when I should do Chest:SetPrimaryPartCFrame()

How would I change the origin orientation in a model?

local map = workspace:WaitForChild('Maps'):FindFirstChild(ChosenMap.Name)
local chestSpawns = map:WaitForChild('chestSpawns'):GetChildren()
local chestFolder = workspace:WaitForChild('ChestFolder')
function SpawnChests()
	chestFolder:ClearAllChildren()
	local setLocations = {}
	for i = 1,3 do
		local chest = game:GetService('ServerStorage').Items.chest:Clone()
		chest.Parent = chestFolder
		for _,chests in pairs(chestFolder:GetChildren()) do
			if chests:IsA('Model') and chests.Name == 'Chest' then
				local function FindValidLocation()
					local location = chestSpawns[math.random(1,#chestSpawns)]
					if not table.find(setLocations,location) then
						table.insert(setLocations,location)
						chest.PrimaryPart = chest.Chest_Spawn
						chest:SetPrimaryPartCFrame(location.CFrame)
					else
						FindValidLocation()
					end
				end
				FindValidLocation()
			end
		end
	end
end
SpawnChests()
1 Like

Oh, you’re recalling the function that is way better than making a repeat loop.

How would I change the origin orientation to the spawn orientation?