How would I spawn random parts in the ocean

Hello!

So basically what I want to do is take the models from this folder:
image

And randomly spawn them around this map.

I want it to randomly choose a model and spawn it in this ocean. How would I achieve this and is it also possible to spawn another model after the other one is destroyed.

For parts try use maybe

math.random(1,10) -- for example

Would this work for models too?

No that won’t just try to get an exact number for each model
like
Can -1
Model - 2

local Number = 0
while true do
Number = random.math(1,2)
if Number == 1 then
--Clone and position script
if Number ==2 then
--Clone and position script
wait(0.1) 

I do also want the models to spawn in random positions too, would I have to get every position in the map

No just use random math to randomize position

It is saying unknown global random,
image

math.random

not random

Learn how to generate a random point within a volume(space). once you learn how to generate that point, you can use a random-number-generator like math.random(min, max) replacing min and max with the range of how many models you have.

math.random(1, 10) --1 being the first element in the list, and 10 being the last`

The reason you do it like this is because youre going to want a table of all the models in TrashCollection(Which you can get by doing)

local TrashCollection = workspace.TrashCollection

TrashCollection:GetChildren()

Then you can either run it through a for loop if your running once, or create a timer and run it through one of the RunService functions.
IF YOURE RUNNING IT ON THE SERVER USE

local RunService = game:GetService("RunService")

RunService.Heartbeat:Connect(function()
    --Spawn Code
end)

IF YOURE RUNNING IT ON THE CLIENT USE

local RunService = game:GetService("RunService")

RunService.RenderStepped:Connect(function()
    --Spawn Code
end)

or

local RunService = game:GetService("RunService")

RunService:BindToRenderStep("whatever name you want", *A RENDER PRIORITY*, function()
    --Spawn Code
end)

So just like this?

math.random(1, 10) --1 being the first element in the list, and 10 being the last`

local TrashCollection = game.ServerStorage.TrashCollection

TrashCollection:GetChildren()

local RunService = game:GetService("RunService")

RunService.Heartbeat:Connect(function()
	TrashCollection:Clone()
end)

No. Trash Collection returns a table of all the objects in TrashCollection.
It would look like this

[TrashCollection]
> Model1
> Model2
> Model3
> Model4
> etc...

and math.random will generate a number between whatever min and max you set, but it needs to generate a new number every frame if you want it to always be spawning at a new position. You also need a way to keep track of how many models are being spawned and how often so that you dont cause your game to lag.

it would look more like this

local RunService = game:GetService("RunService")

local TrashCollection = workspace.TrashCollection

RunService.Heartbeat:Connect(function()
    local entities = TrashCollection:GetChildren() --this gives you a table of all the entities in TrashCollection
    local random_num = math.random(1, #entities) --putting a # next to a table value gives you the size of the table

    local random_position = GenerateRandomPoint() --This isnt a real function and is here for demonstration purposes

    local entity = entities[random_num] --the 2 brackets [] let use index a value in the table using a number or a name(if it has a name)

    entity:SetPrimartPartCFrame(CFrame.new(random_position))
    
end)

this is as indepth as ill go, so youll have to figure out the rest on your own. lmk if you have any more questions on this

So What would I put at

  local random_position = GenerateRandomPoint() --This isnt a real function and is here for demonstration purposes

then?

Also thank you so much for helping me

thats where you have to learn how to generate a point in a volume, you can use google to figure it out. its not super hard.
You can either generate a point in a Box, or a Sphere, those are the 2 easiest

As a stretch goal (ie after you get the randomPoint working), you can think about how to generate x random positions so they’re mostly spread apart. Right now, if you want to make only 10 random positions there’s no guarantee they’ll be y units apart.

There are solutions to generating a point that can divide the positions uniformly

local Server = game:GetService("ServerStorage")
local TrashFolder = Server.TrashCollection

for _, Trash in ipairs(TrashFolder:GetChildren()) do
	task.wait()
	local TrashClone = Trash:Clone()
	Trash:PivotTo(CFrame.new(0, 0, 0)) --This is what you'd need to randomise.
	TrashClone.Parent = workspace
end

This is actually relatively simple to achieve.
If you also want to spawn a random amount of everything then you’d incorporate a for loop into that previous implementation.

local RandomObject = Random.new(tick())
local Server = game:GetService("ServerStorage")
local TrashFolder = Server.TrashCollection

for _, Trash in ipairs(TrashFolder:GetChildren()) do
	for Index = 1, RandomObject:NextInteger(10, 100) do --Spawn each "part" randomly between 10-100 times.
		task.wait()
		local TrashClone = Trash:Clone()
		Trash:PivotTo(CFrame.new(0, 0, 0)) --This is what you'd need to randomise.
		TrashClone.Parent = workspace
	end
end