Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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

1 comment

0

Thank you, this helped alot!

This discussion is preserved as it was. New answers, comments, and votes are closed.