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

Why does calling one function automatically stop any further to be called?

Asked by 7 years ago
local lighting = game.Workspace.Model
local light = lighting.LightPart

local timeOutput = game.Lighting:GetMinutesAfterMidnight()


local nightStandard = 6

local dayStandard = 18


local num = 2

local function DayNightCycle()
    while wait(.2) do
        num = num + 1
        game.Lighting:SetMinutesAfterMidnight(num *60)
        print(game.Lighting.TimeOfDay)
    end
end

local function bindToTime()
    print("binded")
end





while wait(1) do bindToTime() end

DayNightCycle()


For some explanation, I'd like to add that switch the bindToTime function being called and DayNightCycle does matter, because if I add DayNightCycle() first, bindToTime() won't work. How can I prevent from one function being called automatically stopping the entire script?

1 answer

Log in to vote
0
Answered by 7 years ago

A while loop basically prevents anything underneath it from running, as Lua compiles (runs) the code from top to bottom.

to fix this, you can use a thing called "spawn".

local lighting = game.Workspace.Model
local light = lighting.LightPart

local timeOutput = game.Lighting:GetMinutesAfterMidnight()

local nightStandard = 6

local dayStandard = 18

local num = 2

local function DayNightCycle()
    while wait(.2) do
        num = num + 1
        game.Lighting:SetMinutesAfterMidnight(num *60)
        print(game.Lighting.TimeOfDay)
    end
end

local function bindToTime()
    print("binded")
end

spawn(function() while wait(1) do bindToTime() end end)

DayNightCycle()

0
Thank you, this helped alot! good_evening 7 — 7y
Ad

Answer this question