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

How to let a script continue running, while also having wait statements inside?

Hi, I'm sure that this kind of question has been asked before, but I don't know the right terms to search it up. I struggle to understand things like threads and things like that.

I'm trying to make a rain-like script. It's probably inefficient, but thats part of the learning experience.

	while ( true ) do
			local droplet = Instance.new( "Part" )
			droplet.Parent = workspace.Rain
			droplet.Name = ' droplet '
			droplet.CanCollide = false
			droplet.Size = Vector3.new( 0.2, 1, 0.2 )
			droplet.Position = Vector3.new( math.random(0,10), math.random(0,10), math.random(0,10) )
			droplet.Velocity = Vector3.new ( -10, 0, 0 )
			droplet.Orientation = Vector3.new( 0, 0, -40 )
			droplet.Color = Color3.fromRGB( 91, 135, 217 )
			droplet.Transparency = 0.64
			
			droplet.Touched:Connect( function ( collided )
				if ( collided.Name == ' droplet ') then return end
				print('Splash!')
				-- A wait() will probably be inserted here later to wait for the droplet to partially insert itself into the ground, instead of instantly vanishing.
				droplet:Destroy()
			end )
		
		wait ( math.random( 0.001, 0.05 ) )
	end

The problem is that that when I try to wait() before creating a new instance of a droplet, it will suspend all other droplets which are in the air at that time. I don't know how to get around this.

I've tried looking at things like delay() and spawn(), but I cannot get it to do what I want.

1 answer

✓ Accepted answer

just add a wait statement directly in the while loop..

like this:

while ( wait() ) do
        local droplet = Instance.new( "Part" )
        droplet.Parent = workspace.Rain
        droplet.Name = ' droplet '
        droplet.CanCollide = false
        droplet.Size = Vector3.new( 0.2, 1, 0.2 )
        droplet.Position = Vector3.new( math.random(0,10), math.random(0,10), math.random(0,10) )
        droplet.Velocity = Vector3.new ( -10, 0, 0 )
        droplet.Orientation = Vector3.new( 0, 0, -40 )
        droplet.Color = Color3.fromRGB( 91, 135, 217 )
        droplet.Transparency = 0.64

        droplet.Touched:Connect( function ( collided )
            if ( collided.Name == ' droplet ') then return end
            print('Splash!')
            -- A wait() will probably be inserted here later to wait for the droplet to partially insert itself into the ground, instead of instantly vanishing.
            droplet:Destroy()
        end )
end

the physics engine will continue running regardless of where a wait statement in your code.. therefore you dont need to worry about other droplets freezing in time b/c of a wait statement

2 comments

0

Thank you! I think I may have been thinking about it the wrong way. I think the reason they appeared suspended in mid air was because I had their position on the Y axis randomized, and when they first spawn in they appear frozen. Sorry for the inconvenience!

0

No problem (:

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