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 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
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.
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?
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.”
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).