Spawning System not working?

Hello! I am trying to create a system that randomly spawns parts into the ocean around the map. Currently it is not working and IS NOT GIVING ANY ERRORS

Code:

local trash = game.ReplicatedStorage.Content.Trash
local blacklist = {}

local raySettings = RaycastParams.new()
raySettings.FilterDescendantsInstances = blacklist
raySettings.FilterType = Enum.RaycastFilterType.Blacklist

-- System:

local function selectTrash()
	if #trash:GetChildren() > 0 then
		return trash:GetChildren()[math.random(1, #trash:GetChildren())]
	end
end

local function getTrashToCollect()
	local lb = 0 -- Make a new value
	for _,trash in pairs(workspace.World.Trash:GetChildren()) do -- Loop thru the trash
		lb += trash:GetAttribute("Weight") / 2 -- Add weight
	end

	--[[workspace.Terrain.WaterColor = Color3.fromRGB(2, 54, 20):Lerp(Color3.fromRGB(20, 89, 95), game.ReplicatedStorage.TrashCollected.Value / lb)
	workspace.Terrain.WaterTransparency = 0.25 * (game.ReplicatedStorage.TrashCollected.Value / lb)]]

	lb = math.round(lb)

	return lb -- Return it
end

--[[ DOCUMENTATION

TrashSpawn.new( -- Used for creating rounds, and will be required very simply.
    maxSpawns -- Most amount of trash found in that round
)

TrashSpawn:Modify( -- Used for modifying rounds, maybe if there was something REALLY specific where you need more/less trash.
	maxSpawns -- Most amount of trash found in that round
	enabled -- (OPTIONAL): Decides whether or not the system is active, NOTE: If you are trying to disable it, use :Terminate() this is mostly just for testing purposes.
)

TrashSpawn:Inititate( -- Starts the spawning
)

TrashSpawn:Terminate( -- Ends the round early
)

]]

local TrashSpawn = {}

TrashSpawn.__index = TrashSpawn

function TrashSpawn.new(maxSpawns) -- Create a new TrashSpawn
	local self = setmetatable({}, TrashSpawn)

	self.maxSpawns = maxSpawns

	workspace.World.Trash:ClearAllChildren() -- Clears all the trash from previous rounds if there still is any

	return self
end

function TrashSpawn:Modify(maxSpawns,enabled) -- Allows you to modify a currently running TrashSpawn
	self.maxSpawns = maxSpawns

	if not enabled then
		self.enabled = enabled -- NOT RECOMMENDED, USE :TERMINATE()
	else
		print("Successfuly modified the TrashSpawn.")
	end
end

function TrashSpawn:Initiate(updateAtmosphere) -- Initiates spawning
	self.enabled = true

	local tick1 = tick() -- Use this to record the starting time
	local connection = nil
	connection = game:GetService("RunService").Stepped:Connect(function() -- Start a loop to continue the entire time
		if self.enabled == true then
			if (#workspace.World.Trash:GetChildren() < self.maxSpawns) then -- Make sure there isn't already too many

				local x = Random.new():NextNumber(-2500,2500) -- Random coordinate selection
				local z = Random.new():NextNumber(-2500,2500) -- Random coordinate selection

				local ray = workspace:Raycast(Vector3.new(x,1000,z), Vector3.new(0, -2500, 0), raySettings) -- Create the ray check

				if (ray and ray.Material) then -- Check if it worked
					if ray.Material == Enum.Material.Water then -- Make sure it's over a base

						-- Creation of trash goes here, random amount of "clumps"

						for i=1,Random.new():NextInteger(1,3) do
							local oldTrash = selectTrash()
							if oldTrash then
								local newTrash = oldTrash:Clone()
							newTrash.CFrame = CFrame.new((CFrame.new(ray.Position) * CFrame.new(0, newTrash.Size.Y / 2, 0)).Position, ray.Position + ray.Normal)
							newTrash.CFrame *= CFrame.Angles(
								math.rad(math.random(-360,360)),
								math.rad(math.random(-360,360)),
								math.rad(math.random(-360,360))
							)
							newTrash.Anchored = false
							newTrash.CanCollide = true
							newTrash.Parent = workspace.World.Trash
							newTrash:SetNetworkOwner(nil)
							script.Dampening:Clone().Parent = newTrash
						end
					end
					end
				end

				game.ReplicatedStorage.TrashRequirement.Value = getTrashToCollect()
				updateAtmosphere()
			else -- When all the trash is done
				self.enabled = false

				game.ReplicatedStorage.TrashRequirement.Value = getTrashToCollect()
				updateAtmosphere()

				print("Trash was generated in ".. tick() - tick1.. "!")			
				connection:Disconnect()
			end
		else -- Proccess was already terminated
			warn("Proccess was terminated")
			connection:Disconnect()
		end
	end)
end

function TrashSpawn:Terminate() -- Terminates a currently running trash spawn
	self.enabled = false
end

return TrashSpawn
1 Like

Do you know around where the issue might be? Have you for example used prints to narrow it down?

Also, what exactly “is not working”. Is it:

  • Not spawning anything at all?
  • Spawning in wrong location?
  • Everything spawning in one location?
  • etc.

If you can provide information of what more exactly isn’t working it would be great.
I would greatly appreciate if you are able to narrow the possible fault down a bit.

Tips for trouble shooting:
Print key information from different places, think whether the following prints are logical based on previous prints. It is a good way to both find the area and also find the issue itself.

1 Like

Sorry about not being very specific, it is not spawning at all and I also put a print after the clone line where I clone I got only 1 print but nothing actually spawned

image

One reason you get “only” one print is because of your random number generator. So getting 1 is expected.

However I am not sure why it doesn’t spawn, it didn’t pop-up inside workspace.World.Trash?

Oh shoot I forgot to check the folder, but is there a way to not make it 1 cause its doing 1 every time.

Yeah, its possible. I’ve never used “:NextInteger()”, however you can temporarily comment the following row out and replace it with a new for-loop.

-- change to this
for i=1, <your desired loop-count> do

Hopefully it is there, just in the wrong place. If its in the wrong place it means your CFrame positioning is wrong. If it isn’t there? Well. I don’t know what the issue is then. It’s late here, so might be why I don’t find that issue. If the issue still persists tomorrow I might check it out then.