Is there a way to stop a running function from another function? (Running thread)

I know about return method but this time its not need.

Scenario:

  • local function 1 is running now, at a certain time local function 2 is running to stop a running thread of local function 1. is this possible?

Implemented:

  • As I implement the scenario, I got a different expected scenario which is the local function 1 still running its thread even though local function 2 disconnect it via BindableEvent.

Reason of making:

  • So, I’m making an Intro UI to make a scene before it reaches the Main UI which are the buttons for (Ex. Play, Settings, Load, Etc.). Now, if a player wants to skip the intro, there’s a popup of Skip UI that guide them to use the KeyCode.
  • (If a Player push that KeyCode), local function 2 should Disconnect the local function 1 from a BindableEvent assigned variable.

Example Code:

local TweenService = game:GetService("TweenService")
local BindableEvent = --Instance of BindableEvent

local function FOO()
    local IntroFrame = --Instance
    local Button = IntroFrame.Button
    local Animation = TweenService:Create(Button, TweenInfo.new(5), {Position = UDim2.new(--[[Some values]])})
    Animation:Play()
    Animation.Completed:Wait()
end

local Connection = BindableEvent.Event:Connect(FOO)

local function BAR()
    Connection:Disconnect() -- (O_o)
end

As you can see… BAR() should Disconnect FOO() BUT FOO()'s thread is still running in the background. I want it to stop on that spot when BAR() disconnects it.

Use a promise

Or manually implement a cancel function into the thread (well either way both ways you will have to implement a cancel function).

1 Like

After reading the Promise library, I’m still confuse on how I can implement that on my current code.

Is there a simplest way example to use Promise? (Maybe similar to my example code)

(I just want to see how to implement Promise because Im not that expert in Lua so I need guide)