Moving Model to Workspace breaks script?

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.

In ReplicatedStorage:

In Workspace:
image

Boat Location:

Error:

SpawnerScript:

local function selectTrash()

return trash:GetChildren()[math.random(1, #trash:GetChildren())]

end

Spawning Parts:

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
1 Like

Can you send more of the script? Either I’m blind or you didn’t send the variables being used, specifically the trash variable.

Also, how’d you get your icons to look like that? Mine don’t look like them :frowning:

1 Like

Whole Script

-- 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

1 Like

There is a way that you can change those icons. But I’m not sure if it is allowed.

May you show us game.ReplicatedStorage.Content.Trash in explorer?

Sorry for late reply I fell asleep!

image

By the way can you show us the line that calls the function? I don’t see it in the script or maybe my mistake?

1 Like
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

Is this line “Line 103”? The error line you shown above

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.

yes it is the error line that it showed

yeah but I am not moving the boat to any folder just in plain workspace and the folder its in in replicated storage is not related to it

Try doing this

local function selectTrash()
    print(#trash:GetChildren())
	return trash:GetChildren()[math.random(1, #trash:GetChildren())]
end

Do I move the boat or leave it in replicated storage?

When I leave it in Replicated Storage:
image

When I leave it in workspace:

So does it work when you leave it in ReplicatedStorage?

yes it does work when I leave 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)

How would I solve this issue though?

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

Just perform a conditional check beforehand to ensure the trash folder contains children before attempting to select a random child from it.