How to call any function from anywhere inside its script

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I am trying to create a system that allows me to call any of my functions from anywhere inside a script, regardless of if they are called before the function is defined or not.

  2. What is the issue? Include screenshots / videos if possible!
    Doesn’t seem possible

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I’ve tried moduleScripts, but I don’t understand them, and I don’t really want to use object values or remoteEvents.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

-- Here is what i'm trying to do

local function Test1()
      Test2()
end

local function Test2()
      print("this is dumb")
end

Test1()
2 Likes

Making the functions non-local (removing local keyword before function keyword) will resolve your issue.

3 Likes

Would it be okay to do that? I have heard that this would make things unsecure or more vulnerable to hacking

2 Likes

No. Its fine to do it, I’m not sure where you heard that but its safe to do.

1 Like

Sorry one more question. If there is no downside to making global functions, why is it recommended to use local ones instead?

1 Like

I believe it has something to do with memory access and that it may be a little faster but don’t quote me on that.

1 Like

Ok well it is working now so thank you

1 Like

I have adapted this into a semi-efficient form shown here

local function Test1()
      print("this is function one")
      FunctionCaller(2)
end

local function Test2()
      print("this is function two")
end

function FunctionCaller(functionValue)
      local functionArray = {local function() Test1() end, local function() Test2() end}
      functionArray[functionValue]
end

If im right, this should allow you to call any function stored in that array, which is at least somewhat simple and easy to organize with.

1 Like

Why not just

local Functions = {}

function Functions.Test1()
	print("This is function one")
	Functions.Test2()
end

function Functions.Test2()
	print("This is function two")
end

because I am stupid, I might do that. the one thing is that having a global function might be beneficial for something else

One question, those functions don’t need to be local right? Im pretty sure this is just like the array of functions I made earlier except expanded

Well what do you mean by local? If you want these to be global just make it a module script in any directory accessible by both client and server and call require on it. I’m not really sure what you meant

I meant that it just says function functions.test1 and not local function functions.test1. I am pretty sure I know why this is but i’m just making sure.

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