How would I make this "raining system" repeat again?

Hello! I’ve been trying to make this “raining system” repeat (meaning after it’s done raining, it will repeat again, it will go back to line 1 and repeat or it will go to the top of the while loop and repeat), however I haven’t gotten in to work. Haven’t touched coding in awhile… :stuck_out_tongue:

Everything works as well, as it should.

Code:

local RainSpawned = 0

wait(math.random(1, 100))
print("It has started raining!")
while true do
	if RainSpawned >= 1000 then
		break
	end
	local RandomNumber = math.random(1, 50) 
	local RandomNumber2 = math.random(1, 50)
	local RandomNumber3 = math.random(1, 50)
	RainSpawned = RainSpawned + 1
	wait()
	local Rain = Instance.new("Part")
	Rain.Size = Vector3.new(0.2, 3.5, 0.2)
	Rain.Position = Vector3.new(RandomNumber, RandomNumber2, RandomNumber3)
	Rain.Anchored = false
	Rain.CanCollide = false
	Rain.Color = Color3.fromRGB(180, 210, 228)
	Rain.Parent = workspace.Droplets
end
print("It has stopped raining!")

Let me know if you have a solution to this, thanks!

3 Likes

You may want to add a wait(1) at the end of the while loop :stuck_out_tongue_winking_eye:

1 Like

The problem with that is it makes the rain delayed by one second, meaning there will only be one drop of rain every second.

I might’ve not have been clear enough lollll, made a change at the top if you wanna take a look at it. :man_facepalming: :laughing:

Ah i see. Look at this article: Introduction to Scripting | Documentation - Roblox Creator Hub at the Repeat until. That will fix the issue

1 Like

Thinking you can use .Heartbeat, or just try using wait() as it still puts a small delay on it.

You can read up on .Heartbeat here: RunService | Documentation - Roblox Creator Hub. A yield is essential in this case as no yield can cause extreme lag and crashing, as well as script overload.

Also, just for organization’s sake, you should use while RainSpawned <= 1000 do instead of break in this situation. Alternatively, you could use a for loop like this:

for i = 1, 1000 do -- i is the amount of iterations, 1 is what it starts at, and 1000 is where it ends. You can also use the i variable anywhere within the loop.
--stuff
end
1 Like

just use wait(), as that is a fraction of a second.

2 Likes

I think it’s better to use Heartbeat, since it’s more precise and less laggy

2 Likes

Well you could do a while wait(math.random(1,100)) do instead of wait(math.random(1,100))

(and I recommend you using a repeat until loop instead of while true do in that case so you can remove the if RainSpawned thing and just do repeat until RainSpawned >= 1000)

1 Like

Pretty sure I did something wrong, it starts to raining in-game but only a few drops, then after a few seconds it starts pouring. I was hoping to make it so that every time before it starts raining, it does a math.random number then counts down from that number (between 1-100, but in this case 1-10 for testing purposes) once it counts down from that number it starts raining. Then when the rain stops it repeats the same process by picking a random number then counting down from that number. Pretty sure I was the one who made a mistake in setting up the heartbeat service.

local RunService = game:GetService('RunService')
local RainSpawned = 0
local RainEnd = 1000

RunService.Heartbeat:Connect(function(step)
	print("It has started raining!")
	for i = 1, 10 do
		wait(math.random(1, 10))
			while RainSpawned <= RainEnd do
				RainSpawned = RainSpawned + 1
			print("Time between each loop: "..step)
		end
	end
			
	local RandomNumber = math.random(1, 50) 
	local RandomNumber2 = math.random(1, 50)
	local RandomNumber3 = math.random(1, 50)
	RainSpawned = RainSpawned + 1
	wait()
	local Rain = Instance.new("Part")
	Rain.Size = Vector3.new(0.2, 3.5, 0.2)
	Rain.Position = Vector3.new(RandomNumber, RandomNumber2, RandomNumber3)
	Rain.Anchored = false
	Rain.CanCollide = false
	Rain.Color = Color3.fromRGB(180, 210, 228)
	Rain.Parent = workspace.Droplets
			
	print("It has stopped raining!")
end)

The issue seems to be your loop. You want the whole script to loop, not just those few lines. Try something like this and see if it’s any better:

local RunService = game:GetService('RunService')
local RainSpawned = 0
local RainEnd = 1000

RunService.Heartbeat:Connect(function(step)
	print("It has started raining!")
	for i = 1, RainEnd do -- keeps going until RainEnd (1000)
		wait(math.random(1, 10))
		print("Time between each loop: "..step)
			
	local RandomNumber = math.random(1, 50) 
	local RandomNumber2 = math.random(1, 50)
	local RandomNumber3 = math.random(1, 50)
	RainSpawned = RainSpawned + 1
	wait()
	local Rain = Instance.new("Part")
	Rain.Size = Vector3.new(0.2, 3.5, 0.2)
	Rain.Position = Vector3.new(RandomNumber, RandomNumber2, RandomNumber3)
	Rain.Anchored = false
	Rain.CanCollide = false
	Rain.Color = Color3.fromRGB(180, 210, 228)
	Rain.Parent = workspace.Droplets
			
	print("It has stopped raining!")
	end
end)
1 Like

woahhhh

Also, it seems to endlessly rain.

Ohhhh I see the issue I think.

Every single time a heartbeat happens, it will connect to the function. You should use the .Heartbeat as a wait, not to connect to the function. Try something like this instead:

while true do
	print("It has started raining!")
	for i = 1, RainEnd do -- keeps going until RainEnd (1000)
		wait(math.random(1, 10)) -- keep in mind that this is only going to create a raindrop each 10 seconds.
		print("Time between each loop: "..step)
			
	local RandomNumber = math.random(1, 50) 
	local RandomNumber2 = math.random(1, 50)
	local RandomNumber3 = math.random(1, 50)
	RainSpawned = RainSpawned + 1
	wait()
	local Rain = Instance.new("Part")
	Rain.Size = Vector3.new(0.2, 3.5, 0.2)
	Rain.Position = Vector3.new(RandomNumber, RandomNumber2, RandomNumber3)
	Rain.Anchored = false
	Rain.CanCollide = false
	Rain.Color = Color3.fromRGB(180, 210, 228)
	Rain.Parent = workspace.Droplets
	end
print("It has stopped raining!")
wait(1000) -- time to wait between each rainfall, probably should be a random number but use 1,000 as a starting point. This could also go at the beginning of the loop if you'd like.
end

Also, excuse my poor indenting on here, it’s difficult for some reason :stuck_out_tongue:

2 Likes