How to make a part not spawn if it is already being occupied?

Hello so I am making this spill script where a spill/trash is spawned in a location, so someone can clean it up. I want to continue to spawn the trash until either all of them are occupied so the person can clean it up and then wait a few seconds and trash will pop up again, here is the script. The problem is that there are more than one spills in one certain location. Here is the script.

Server Script

local collectionService = game:GetService("CollectionService")
local spillChoices = game:GetService("ReplicatedStorage"):WaitForChild("SpillTypes")
local spawnChoices = game:GetService("Workspace"):WaitForChild("randomSpawnAreas")

local spillModule = require(script.Parent.Spills)

local spillItems = {}
local areaPlaces = {}

for i, v in pairs(spillChoices:GetChildren()) do
	
	if v.Name == "Spill" then
		table.insert(spillItems, v)
		
	end
end

for i,v in pairs(spawnChoices:GetChildren()) do
	
	if v.Name == "AreaSpawn" then
		table.insert(areaPlaces, v)
		
	end
end

while wait(3) do
	
	local randomSpawnArea = areaPlaces[math.random(1, #areaPlaces)]
	local randomSpill = spillItems[math.random(1, #spillItems)]
	
	local createSpill = spillModule.New(randomSpill, randomSpawnArea)
	
end

Module Script

local Spills = {}

Spills.__index = Spills

function Spills.New(model, location)
	
	local self = setmetatable({}, Spills)
	self.Model = model
	
	self.Model.Parent = game.Workspace
	self.Model:SetPrimaryPartCFrame(CFrame.new(location.Position))
	
	return self
	
end

return Spills
local randomSpawnArea = areaPlaces[math.random(1, #areaPlaces)]

You’re getting a random number that can repeat. I think you can make a table with not occupied area places and then use this method.

How would i do that?
(character limit)

Server Script

local collectionService = game:GetService("CollectionService")
local spillChoices = game:GetService("ReplicatedStorage"):WaitForChild("SpillTypes")
local spawnChoices = game:GetService("Workspace"):WaitForChild("randomSpawnAreas")

local spillModule = require(script.Parent.Spills)

local spillItems = {}
local areaPlaces = {}
local freeAreaPlaces = {} -- Table for not occupied area places

for i, v in pairs(spillChoices:GetChildren()) do
	
	if v.Name == "Spill" then
		table.insert(spillItems, v)
		
	end
end

for i,v in pairs(spawnChoices:GetChildren()) do
	
	if v.Name == "AreaSpawn" then
		table.insert(areaPlaces, v)
		table.insert(freeAreaPlaces, v) -- contains same thing but will be modified
		
	end
end

local random -- will be used
while wait(3) do
	
	random = math.random(1, #freeAreaPlaces) -- got it
	local randomSpawnArea = freeAreaPlaces[random] -- switched to new table
	table.remove(freeAreaPlaces, random)
	-- And after that you need to add these values back when it's no longer occupied by adding the specific area place. If it's managed from the other script you can use an event.

	local randomSpill = spillItems[math.random(1, #spillItems)]
	
	local createSpill = spillModule.New(randomSpill, randomSpawnArea)
	
end
1 Like

I got this error: ServerScriptService.Clean System.Server:32: invalid argument #2 to ‘random’ (interval is empty)

Also the spills disappear and re-locate to another location

Try to remove local areaPlaces = {} and table.insert(areaPlaces, v)

1 Like

Got the same error and the spills are dissapearing and re-locating

I don’t know why you’re getting this error. Works fine for me.
I see this could be caused by no values for math.random what means that freeAreaPlaces table is empty? I tested this code and prints correctly and the table is filled as should be.

1 Like

I have 5 spill choices and 5 spawn locations, could that be the problem?

I fixed the problem where the spills dissapear and re-locate, I just added Clone to the randomSpill variable. The only problem now is the error that keeps popping up. ServerScriptService.Clean System.Server:31: invalid argument #2 to ‘random’ (interval is empty) Maybe it’s because the spill cannot find a place to locate?

Like I said above I don’t know why this error is popping up to you. Also is this error showing to you from start or later on?

The error is showing up later on once all the locations have been filled.

That’s why. When providing a script I wrote there “And after that you need to add these values back when it’s no longer occupied by adding the specific area place. If it’s managed from the other script you can use an event.”

1 Like

How would I be able to add them back?

Call a RemoteEvent from LocalScript when player collects the thing with the instance (given thing). Listen to this event in this script and add the given instance to the table. Of course you can add some verification to it to check that for example it is what you want (randomSpawnAreas.Parent).

1 Like

Alright, thanks for your help!

1 Like