Stack overflow function

Script
	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

I don’t know why it won’t stop. There are several locations and it shouldn’t be using all the locations.

1 Like

Just add a wait() inside the function:

	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
							wait()
							FindValidLocation()
						end
					end
					FindValidLocation()
				end
		end

If this helped please close the topic! :smiley:

@OP
This problem is coming from using a recursive function and that function never reaching a end condition.

While adding a wait would remove the error, it won’t fix the key problem with the code. You should change your algorithm to get chest locations.

Instead of randomly getting chest locations until you find one that isn’t taken, you should loop through your setLocations table and remove all of it’s elements from a duplicate of the chestSpawns table.

Then you should check if there are any remaining locations (aka length of resulting table > 0) and randomly pick a location from the table.

Edit:

Example code for making that table:

local openLocations = {}
for _, location in ipairs(chestSpawns) do
    if not table.find(setLocations,location) then
        table.insert(openLocations, location)
    end
end


if openLocations > 0 then
    -- Open location
else
    -- No open location, potentially remove chest(?)
end

(I believe this is what your code is doing, let me know if I’m wrong though)

1 Like

How would I do this I don’t really understand what your saying.

2 Likes

Within the context of your code block, the code above would look like this:

	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 openLocations = {}
					for _, location in ipairs(chestSpawns) do
						if not table.find(setLocations,location) then
							table.insert(openLocations, location)
						end
					end


					if openLocations > 0 then
						-- Open location
						table.insert(setLocations,location)
						chest.PrimaryPart = chest.Chest_Spawn
						chest:SetPrimaryPartCFrame(location.CFrame)
					else
						-- No open location, potentially remove chest(?)
						--chest:Destroy() -- ??
					end
				end
			end

Edit:

I don’t think so, that’s where it’s making the openLocations table. It checks if the location is not in setLocations, which is where the taken/unopen locations are.

1 Like

do I put if opelocations inside if not table.find there’s an error that’s says unknown global location?

why is it stack overflowing? There’s 7 spawns so I don’t understand.

When there are no spawns left it looks for a spawn forever without stopping.

Which table?

You can use table.clear to clear a table. Could you explain what you’re trying to do?

I can’t see the code that does this stuff, but I think you’d just need to call table.clear(chestSpawns) after you’re finished spawning chests.

Do you want to change chestSpawns each time you call the function to spawn chests?

1 Like

how do I clear the table after it is done spawning all the chest table with the spawn locations so this is in a gamemodule in the gamemodule it calls the function to spawn chest and I need to clear the table with the spawn locations after all the chest are done spawning

1 Like

I found a better solution! And fixes the problem

Script
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)
						elseif table.find(setLocations,location) then
							FindValidLocation()
						end
					end
					FindValidLocation()
				end
		end

It is like a little verifier to check if it is taken.

1 Like