Question about this function setup

Quck question about this method im thinking about using

I know theres task.spawn but i was lazy and didnt look into it and came up with this idea instead

local function a() end
local function b() end

b = function()
	a()
end

a = function()
	print("hello")
end

b()

this works and i want to use it but i just want to know if theres any downsides to what im using or if i should just stick to using task.spawn()

task.spawn() is used to run code in a separate thread, so that it can run at the same time as other things without yielding.

What you have there is just a weird way to run one function after the other. There is no new thread created, it’s just in sequence like everything else.

What are you trying to do here?

1 Like

i really just want to be able to call functions in other functions regardless of placement

so i just create a empty function at the top of the script then actually set the function later so i can just call the function in another function w/o having to place the called function above it

What do you actually gain from creating a blank function then assigning it some code to run later? Why not just create the function and write the code for it at the same time

1 Like

Looks like you’re trying to have functions accessible in code snippets above where they’re defined, but you’re also trying to avoid adding the function to the script’s environment/make the function global.

This seems like a code layout issue and could probably be fixed with some reformatting. But, if you really want to do it this way, you can just do this:

local func

local function a()
    func()
end

func = function()
    print("hi")
end

a() --> hi

If you want more safety, you can check to see if it’s been modified:

local function a()
    if (type(func) == "function") then func() end
end
1 Like

If you make a function or variable without the local keyword, you can access the contents anywhere in the script as long as the code has ran and assigned a value to it.

function a()
 b()
 print(hi)
end

hi = 5
function b()
end
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.