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

Stopping Functions from continuing?

Asked by
Tor6 0
10 years ago

Is it possible to make a function immediately stop, or become nil or something?

3 answers

Log in to vote
4
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago

When a function returns a value, it exits and produces that value as result.

A function may also return nothing (which will be nil), which will still exit the funciton.


Loops (for, while, repeat) have a similar functionality in break which just exits the current loop. return exits the current function, unirregardlessly of how many loops / blocks the statement is in:

function running()
    i = 0
    while true do
        i = i + 1
        if math.random() < 0.0001 then
            return i
        end
    end
end

print( running() )
-- a large number will be printed
Ad
Log in to vote
0
Answered by 10 years ago

I'm not sure about functions, but you can stop loops with break.

For example, if you wanted a for loop to end if a condition is met, you could use this code:

for i,v in pairs(game.Players:GetChildren()) do
    if game.Workspace.Condition.Value == true then
        break
    end
end

Read more on the Wiki.

Log in to vote
-2
Answered by
Thetacah 712 Moderation Voter
10 years ago

functionname:disconnect()

0
Functions don't have a `disconnect` method. Even if they did, this couldn't stop current execution. BlueTaslem 18071 — 10y
0
This is only for Event connections, not functions. Tkdriverx 514 — 10y

Answer this question