Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 6 years ago
Edited 6 years ago

[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)
0
change the = to ==, = assigns a value and == is checking the value against something(more or less) DinozCreates 1070 — 6y
0
At line 10 you got to use Vector3 so, base.Position = Vector3.new(-2.525, 16.18, -12.125) Is_Hunter 152 — 6y
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) Zobot4 0 — 6y

3 answers

Log in to vote
2
Answered by
yHasteeD 1819 Moderation Voter
6 years ago
Edited 6 years ago

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
0
Holy crap. Too much to learn. Give me a few minutes and lemme try to understand FadedJayden_Dev 118 — 6y
0
You can see in the wiki to understand more. yHasteeD 1819 — 6y
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! FadedJayden_Dev 118 — 6y
0
Hmmm it didnt stop. I shall try to learn more about UDim2 and fix it by my own :D FadedJayden_Dev 118 — 6y
View all comments (13 more)
0
I got one error, you need to use Vector3 not UDim2 lol, now fixed yHasteeD 1819 — 6y
0
Lmao I was wondering wasnt it used for guis xD thanks for confusing me xd jk FadedJayden_Dev 118 — 6y
0
For Gui's detect with UDim2, for Part's detect with Vector3 yHasteeD 1819 — 6y
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. FadedJayden_Dev 118 — 6y
0
are you using >= or ==? DinozCreates 1070 — 6y
0
This error is because this is adding too fast, so no time to check if the position is equal to ... yHasteeD 1819 — 6y
0
So what should I do? FadedJayden_Dev 118 — 6y
0
Increase time. and also adjust for the position to go to the position you put. yHasteeD 1819 — 6y
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. FadedJayden_Dev 118 — 6y
0
Put the check inside of the add position yHasteeD 1819 — 6y
0
Huh? FadedJayden_Dev 118 — 6y
0
Do not use while wait(t) do. User#24403 69 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

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
0
Thank you! Will try it now :D FadedJayden_Dev 118 — 6y
1
explain the script and correct errors of the person who asked, This will give you a much better response. yHasteeD 1819 — 6y
0
don't spoonfeed lucldIy 3 — 6y
0
I manage to correct the errors and it just did not work FadedJayden_Dev 118 — 6y
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

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

2) 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

Answer this question