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.
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
@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)
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.
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
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.