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

Calling a function inside a function and continuing itself?

Asked by 6 years ago
local Part = game.Workspace.Part

function onOne()
    onTwo()
    Part.Transparency = 1
end

function onTwo()
    wait(1)
    Part.Transparency = 0
end

onOne()

Ofcourse, when you call function onTwo, onOne stops. What I want to know is: How can I call onTwo and continue onOne?

0
It doesn't 'stop', it waits for ontwo to finish User#20388 0 — 6y

2 answers

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

Another person had asked a similar question. I had replied with the following example. I don't really understand what you are looking for but take a look at my example. Comment below if that is not what you are looking for.

To show you an example, I have 2 Bricks in Workspace, named ClickBrick and ChangeColour. When clicked on the ClickBrick it will result in printing, "Brick is Clicked". Then when clicked on the ChangeColour Brick, the brick will change into red colour.

When you press the ChangeColour Brick first, it will not change its colour because it is inside the Click Brick's code.

-- Declaration Section 
--//Game Services 
local Workspace = game:GetService("Workspace")

--//Assets 
local button = Workspace:WaitForChild("ClickBrick")
local ColourChanger = Workspace:WaitForChild("ChangeColour")

-- Processing Section

changePartColour = function()
    print("It is clicked!")
    local function changeBrickColour ()
        ColourChanger.BrickColor = BrickColor.Red()
        print("Changed to red colour!")
    end
    ColourChanger.ClickDetector.MouseClick:Connect(changeBrickColour) --You don't need to connect it here, you can also connect it in line 13. 
    print("Continue your code below here.")

    wait(5)

    ColourChanger.BrickColour = BrickColor.Gray()
end

button.ClickDetector.MouseClick:Connect(changePartColour)

There is another technique in how you would perform the same task. Spawn is used to embed another function inside a function

-- Processing Section 

local function PrintSomeText()
    spawn(PrintText)
    print("This printed")
end 

local function PrintText ()
    wait(1)
    print("Hello World!")
end 

PrintSomeText()

Is this what you wanted? Let me know if it didn't help you.

Ad
Log in to vote
0
Answered by 6 years ago

The onTwo function has a wait inside, so you should spawn it. Let me give you the fixed code:

local Part = game.Workspace.Part

function onOne()
    spawn(onTwo)
    Part.Transparency = 1
end

function onTwo()
    wait(1)
    Part.Transparency = 0
end

onOne()

Answer this question