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

[Solved]How do I break a loop after a condition is met?

[EDIT] I managed to used 'for i' loop and CFrame to do what I needed to do.

Sorry if the title was misleading cause I have no idea what title to give it

I need help trying to do an if then break loop. Like if a part reaches a specific coordinate, it will break the loop which makes it stop moving.

Here is my script and at line 10, it underlines the '=' sign. It says 'Expected then, got ='

local base = game.Workspace.WaitForChild("Elevatorbase")
local b2 = script.Parent

function two()
while true do
		wait(0.01)
	base.Position = base.Position+Vector3.new(0, 0.07, 0)
end
if 
base.Position = -2.525, 16.18, -12.125
then 
	break

end

script.Parent.ClickDetector.MouseClick:Connect(two)

3 comments

0

change the = to ==, = assigns a value and == is checking the value against something(more or less)

0

At line 10 you got to use Vector3 so, base.Position = Vector3.new(-2.525, 16.18, -12.125)

0

im sure the if statement has to be inside the while true loop and change the equal sign on line 10 to > because if you just use a equal sign i could possibly go higher than that number and loop forever but the > sign will stop it if it goes above then it will stop and also change the value on line 10 to Vector3.new(-2.525, 16.18, -12.125)

3 answers

You need to detect position with item.Position == Vector3.new(x,y,z) not item.Position = x,y,z You simply used the wrong operator for this. And you forgot to put the break inside loop

Here is fixed script:

local base = workspace:WaitForChild("Elevatorbase") -- You dont need to use, and use :WaitForChild not .WaitForChild, game.Workspace..., you can use workspace...
local b2 = script.Parent

function two()
	while true do
		base.Position = base.Position + Vector3.new(0, 0.07, 0)
		
		if base.Position == Vector3.new(-2.525, 16.18, -12.125) then -- You needed to put this inside the place where the loop is, not out of loop
			--[[
				do not use:
				if
					... = ...
				then
					...
				end
					
				use:
				if ... == ... then
					...
				end
			--]]
			break
		end
	wait(0.01)
	end -- You forgot to put this end
end

script.Parent.ClickDetector.MouseClick:Connect(two)

Examples of conditional statsments:

-- 1 > Detect if is equal
if 2 + 3 == 5 then
	print("two plus three is five")
end

--> Detect another equal example: (by: me)
local number = 0

if number == 0 then
	print("Number is 0 ")
else
	print("Number is not 0, the number is: " .. tostring(number))
end


-- 2 > ~= operator
if 100 ~= 4 then -- the '~=' operator means 'not equal to'
	print("100 is not equal to 4")
end

-- 3 > Else
if 10 > 100 then 
    print("10 is greater than 100") 
else 
    print("10 is less than 100") 
end

-- 4 > Elseif
if 10 > 100 then 
    print("10 is greater than 100") 
elseif 10 > 50 then	
    print("10 is greater than 50") 
elseif 10 < 100 then	
    print("10 is less than 100") 
end

-- 5 > Detect bool values
local	pizzaPlaceIsOpen = false
local tacoPlaceIsOpen = true
local hotdogStandIsOpen = false

--should output "Taco Place" with the above code
if pizzaPlaceIsOpen == true then
    print("Pizza Place")
elseif tacoPlaceIsOpen then  -- var == true is usually unnecessary when var is a boolean
    print("Taco Place")
elseif hotdogStandIsOpen then
    print("Hotdog Stand")
else
    print("Hungry!")
end

All examples is in roblox wiki.

Roblox wiki pages:

Vector3

Conditional Statements

Break

loops

Hope it helped :D

Solved your problems? put in title [SOLVED] or accept a answer

17 comments

0

Holy crap. Too much to learn. Give me a few minutes and lemme try to understand

0

You can see in the wiki to understand more.

0

I just realized I made many silly errors. Thanks for helping me! Great help. Now I need to learn more on UDim2. You taught me something new :D appreciated!

0

Hmmm it didnt stop. I shall try to learn more about UDim2 and fix it by my own :D

0

I got one error, you need to use Vector3 not UDim2 lol, now fixed

0

Lmao I was wondering wasnt it used for guis xD thanks for confusing me xd jk

0

For Gui's detect with UDim2, for Part's detect with Vector3

0

Gotcha. It still didn't work :v I checked the coordinates and everything was correct. It just kept moving even after reaching the coordinates.

0

are you using >= or ==?

0

This error is because this is adding too fast, so no time to check if the position is equal to ...

0

So what should I do?

0

Increase time. and also adjust for the position to go to the position you put.

0

Help? I've been trying to do it for a very long time here. I tried changing the wait time to 0.1 still no difference.

0

Put the check inside of the add position

0

Huh?

0

Do not use while wait(t) do.

this should work

local base = workspace:WaitForChild("Elevator")
local b2 = script.Parent
script.Parent.ClickDetector.MouseClick:Connect(function()
repeat
 wait(0.01)
base.Position = base.Position+Vector3.new(0,0.7,0)
until base.Position = -2.525, 16.18, -12.125

4 comments

0

Thank you! Will try it now :D

1

explain the script and correct errors of the person who asked, This will give you a much better response.

0

don't spoonfeed

0

I manage to correct the errors and it just did not work

  1. You used the wrong operator (you initialize variables with an equal sign =). If you want to check if two things are equal to each other, you use the == operator. For example:
print('hello' == 'hello')
-- true
  1. You most likely accidentally pressed enter. You use conditional statements like this:
if x then
	... code
end
-- or
if x then code end

and not like this:

if
x
then
	... code
end

I suggest that you take a look at these pages: Operators, If statements

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