Raining parts script isn't working

I am trying to make parts rain from the sky but somehow they don’t appear so can someone show me whats wrong with it?

this is the code:
local Debris = game:GetService(“Debris”)

local partCount = 100

while true do

end

local part	= Instance.new("Part")
part.Parent = game.Workspace.Part

local randomX = math.random(-200, 200)
local randomZ = math.random(-200, 200)

part.Position = Vector3.new(randomX, 50, randomZ)

task.wait(1)

end

Why would you parent it to a part? I suggest parenting it to a Folder instead. Another question: why do you have Debris Service if you don’t use it?

Where are you having trouble with?

  • Only one part is spawned/ No parts are being spawned at all

You ended the loop here

:white_check_mark: Tested your script
everything is alright, 1 question; is this in a local or server sided script.

it is a normal script, I am starting to script so i have alot of troubles.
should i erase “end”?

adding a end below your while script will end the loop, if you want it to continue and spawn the part, yes (only the one you put above local part = Instance.new("Part"))

Other Notes:

If you already placed a while loop in the script (hoisted) you can’t put another script unless you do

spawn(function()
 while true do()

 end
end)
  1. You are ending the while loop right after starting it.
  2. You are parenting the part to another Part, I rather suggest to just parent it to workspace.

Next we can use Debris to delay the destruction of the parts. Also let’s only make 100 parts and then stop, for this we need to make a for loop.

local Debris = game:GetService("Debris")

local partCount = 100

-- i starts at 1, ends at partCount, each time +1 gets added
for i = 1, partCount, 1 do
	local part = Instance.new("Part")
	part.Position = Vector3.new(math.random(-200, 200), 50, math.random(-200, 200))
	part.Parent = workspace

	Debris:AddItem(part, 5) -- part will be destroyed after 5 seconds

	task.wait(1) -- delay until the next rain drop
end

-- Anything below the for loop will wait until the for loop has concluded.
3 Likes

I would probably do something like this:

  1. Create a rain part template
  2. Make a loop that runs once in a while
  3. Inside of the while loop I would clone the RainTemplatePart (created in the 1st point)
  4. Position it randomly using math.random() (just like you did)
  5. Use DebrisService to add the item

It would look something like this:

local DebrisService = game:GetService("Debris")
local RainTemplate = Instance.new("Part") --you can make it inside of workspace and just do local RainTemplate = workspace.RainTemplate
RainTemplate.Size = Vector3.new(1,3,1)
RainTemplate.Transparency = 0.5
RainTemplate.Material = Enum.Material.Glass

local createdRain = 0
local MAX_RAIN = 100
local DELAY = 1

coroutine.wrap(function()--coroutine not needed, you can just use task.spawn()
  while MAX_RAIN > createdRain do
       createdRain += 1
       local randomX = math.random(-200,200)
       local randomZ = math.random(-200,200)
       local rainDrop = RainTemplate:Clone()
       rainDrop.Position = Vector3.new(randomX, 50, randomZ)
       rainDrop.Parent = workspace --you can create a folder where you can store all the instances created
       DebrisService:AddItem(rainDrop, 5)
       task.wait(DELAY)
  end
end)() --call it at the end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.