Why isn't this rain script working properly?

Does the tutorial have a copy and paste of the script?

I will record a video to show what is going on.

I know what’s going on. I need to know what you are trying to acheive.

Next time please indent, it would really help us figure out the problem.

I’m just following the tutorial to practice coding. I’m not making a game or anything. You could say there is an achievement because I’m trying to break the loop.

Please format your code correctly next time. We have available guides on how to do so.

local rainspawned = 0

while true do
    if rainspawned >= 50 then break end
    rainspawned = rainspawned + 1
    local Rain = Instance.new(“Part”,game.Workspace)
    Rain.Size = Vector3.new(0.5,2,0.5)
    Rain.Position = Vector3.new(0,15,0)
    Rain.Anchored = false
    Rain.Transparency = 0.5
    wait()
end
print("The loop is finished")

Your code is logically correct, aside from the fact is executes instantly. I’m guessing the issue is somewhere else and this is not the whole script.

1 Like

I did. I’m not sure why it didn’t work though.

use

             '''
 

     '''

to properly format the code.

I know why, put your if statement at the end

Ok. I’ll see if this will make it function properly now.

You can place it anywhere in the loop and it will still stop it once it reaches 50+ I don’t think that will solve the issue.

Yeah, it didn’t work. The problem has to be with the if statement. It prints out the print statement.

Try changing the wait() to something like wait(1)

2 Likes

That didn’t work either. The print statement also didn’t get printed.

What gets printed? @Politeawar

Did you check the output? Also consider indenting the code and properly formatting it.

The print at the very bottom.

print("The Loop is finished!")

What did you get in output.

local Iter = 0

while true do
	Iter = Iter + 1
	if Iter >= 50 then break end
	local Rain = Instance.new("Part",game.Workspace)
	Rain.Size = Vector3.new(0.5,2,0.5)
	Rain.Position = Vector3.new(0,15,0)
	Rain.Anchored = false
	Rain.Transparency = 0.5
	wait(0.1)
end
print("Done")

This seems to work. “Done” is printed when it reaches 50. I just realized that making it >= 50 is not necessary since it will stop as soon as it hits 50.

You can change that to

if Iter == 50 then break end

It’s the same exact print. But, the thing is, that the while loop for some reason doesn’t break. Like, the rain keeps falling.