Hello, I am trying to make a spawner script but every time I move it from ReplicatedStorage to workspace it spams with errors and does not spawn the parts I want it to spawn.
-- Values and stuff:
local trash = game.ReplicatedStorage.Content.Trash
local blacklist = {}
local raySettings = RaycastParams.new()
raySettings.FilterDescendantsInstances = blacklist
raySettings.FilterType = Enum.RaycastFilterType.Blacklist
-- System:
local function selectTrash()
return trash:GetChildren()[math.random(1, #trash:GetChildren())]
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 newTrash = selectTrash():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
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
It’s probably because when you move the boat to workspace, there is nothing left in the folder so its 0. You cant do math.random(1, 0) so maybe you can add a check so that if there is nothing in the folder it will ignore it.
Then I think @SaturdayScandal has answered your question. When you move the boat to workspace, there is nothing in the Trash Folder, so the amount of items in the Folder will be 0. The second argument in math.random cannot be 0, so it gets error. Hope this help you.
(Edit: please mark this as solution so other developers can refer to it when they have similar problems)