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