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

Repeat until Close() Loop?

I'm trying to make the landing sequence more automatic for some StarWars starfighters I've been working on. Until recently I've used a short for loop to make the fighter descend for several seconds before shutting off the "engines," but if you land from too close or too high from the ground, you'll either glitch into the ground or bounce a few times. Therefore I thought to use a repeat until SomePart.Touched loop to descend until a "landing gear" part touched another part. But... there never seems to be a time when Touched is nil or false, so the fighter just drops. I tried this, but the same thing happens:

--RayCast function to detect when another part is close
function near(start, oclu)
	local ray = Ray.new(start.Position, Vector3.new(0,-4,0))
	local hit, pos = game.Workspace:FindPartOnRay(ray, Plane)
	if hit and pos and (start.Position - pos).magnitude < 5 then
		return true
	end
	return nil
end

--Repeat loop that uses the above function
		--Plane is defined earlier, don't worry
		local forc = Plane.Parts.Engine.BodyVelocity
		local dam = Plane.Parts.Damage
		forc.maxForce = Vector3.new(math.huge, math.huge, math.huge)
		repeat
			forc.velocity = forc.velocity + Vector3.new(0, -2, 0)
			wait(0.1)
		until near(dam, Plane.Parent) == true
		
		wait()
		forc.velocity = Vector3.new(0, 0, 0)
		forc.maxForce = Vector3.new(0, 0, 0)

I don't know if I'm simply missing something that would make the Touched and RayCast checks always be true, or what, so feel free to suggest whichever you think is more efficient.

1 answer

You could connect a function before hand and use a while loop and then disconnect afterwards.

For example:

stop = false
connecty = script.Parent.Touched:connect(function(hit)
	stop = true
end)
while stop == false do
	wait(1)
	print("running")
end
connecty:disconnect()

This will allow it to run until the value becomes true

So, to use it in your code:

local forc = Plane.Parts.Engine.BodyVelocity
local dam = Plane.Parts.Damage
forc.maxForce = Vector3.new(math.huge, math.huge, math.huge)
stop = false
connecty = Plane.Parts.Wheel.Touched:connect(function(hit) --Change to what you want on the plane to be touched to make it stop
	if hit.Name == "Ground" then --Change to the name of what the plane should be hitting to stop
		stop = true
	end
end)
while stop == false do
	forc.velocity = forc.velocity + Vector3.new(0, -2, 0)
	wait(0.1)
end
connecty:disconnect()
wait()
forc.velocity = Vector3.new(0, 0, 0)
forc.maxForce = Vector3.new(0, 0, 0)

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