Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why won't my cloned part spawn on the map?

This Script is supposed to spawn parts "randomly" around the map, but they simply don't, I get no errors when I run the code. What could the problem be?

local rs = game:GetService("ReplicatedStorage")
local items = rs:WaitForChild("Items")
local metal = items:WaitForChild("Metal")
local plank = items:WaitForChild("Plank")
local glass = items:WaitForChild("Glass")
local plasticBottle = items:WaitForChild("plasticBottle")

while wait(4.9) do
	print("yes")
	posXMath = math.random(-250, 250)
	posX = posXMath
	posZMath = math.random(-250, 250)
	posZ = posZMath
end

local function spawnItem(item)
	local itemClone = item:Clone()
	itemClone.Parent = game.Workspace
	itemClone.Position = Vector3.new(posX, 0.5, posZ)
end

while wait(5) do
	local itemMath = math.random(1, 10)
	if itemMath <= 7 then
		print("plank")
		spawnItem(plank)
	elseif itemMath <= 5 then
		spawnItem(glass)
	elseif itemMath <= 3 then
		spawnItem(plasticBottle)
	elseif itemMath == 1 then
		spawnItem(metal)
	end
end

1 answer

✓ Accepted answer

The code after your first while loop won't run because the code will run the while loop forever.

If you want your program to run both while loops at the same time, use coroutines:

local rs = game:GetService("ReplicatedStorage")
local items = rs:WaitForChild("Items")
local metal = items:WaitForChild("Metal")
local plank = items:WaitForChild("Plank")
local glass = items:WaitForChild("Glass")
local plasticBottle = items:WaitForChild("plasticBottle")

local function spawnItem(item)
    local itemClone = item:Clone()
    itemClone.Parent = game.Workspace
    itemClone.Position = Vector3.new(posX, 0.5, posZ)
end

coroutine.wrap(function()
	while wait(4.9) do
	    print("yes")
	    posXMath = math.random(-250, 250)
	    posX = posXMath
	    posZMath = math.random(-250, 250)
	    posZ = posZMath
	end
end)()

coroutine.wrap(function()
	while wait(5) do
	    local itemMath = math.random(1, 10)
	    if itemMath <= 7 then
	        print("plank")
	        spawnItem(plank)
	    elseif itemMath <= 5 then
	        spawnItem(glass)
	    elseif itemMath <= 3 then
	        spawnItem(plasticBottle)
	    elseif itemMath == 1 then
	        spawnItem(metal)
	    end
	end
end)()

A better way of doing is determining the random position inside of your spawnItem function. That way we'll only need one loop instead of two.

local rs = game:GetService("ReplicatedStorage")
local items = rs:WaitForChild("Items")
local metal = items:WaitForChild("Metal")
local plank = items:WaitForChild("Plank")
local glass = items:WaitForChild("Glass")
local plasticBottle = items:WaitForChild("plasticBottle")

local function spawnItem(item)
	local positionX = math.random(-250, 250)
	local positionZ = math.random(-250, 250)

	local itemClone = item:Clone()
	itemClone.Parent = game.Workspace
	itemClone.Position = Vector3.new(positionX, 0.5, positionZ)
end

while wait(5) do
    local itemMath = math.random(1, 10)
    if itemMath <= 7 then
        print("plank")
        spawnItem(plank)
    elseif itemMath <= 5 then
        spawnItem(glass)
    elseif itemMath <= 3 then
        spawnItem(plasticBottle)
    elseif itemMath == 1 then
        spawnItem(metal)
    end
end

1 comment

0

thanks:)

This discussion is preserved as it was. New answers, comments, and votes are closed.