• Calling out a function returns a nil
    7 replies, posted
Hello, I have a function named: function main:GhostEntity(ent) It's a global function, and it should work on all scripts, and when I try to call it using this method: if (main:GhostEntity(tr)) then It returns "main is a nil value" as an error, is there a better way to call functions and have it clear they are global functions and not arguments? I'm pretty sure lua thinks I'm talking about an argument, when I am trying to call out a function, how would I properly call out a function? If you're wondering, main is a table in the code the function is located at, "GhostEntity" is not featured in that table, just for your knowledge. Thanks!
Pastebin your code
I can't, it's an addon from Gmodstore. But it basically goes like this (To Avoid Leaking): local main = {}; function main:GhostEntity(ent) --Code Here end  Is "local main = {};" necessary in my other script even if I'm calling out a function from the main script that's built on it?
If its an addon from gmod store, contact its author for help, if you are the author, you should know (how to code) better.
Well, the author has sadly given up on everything, and I'm trying to use his code. It doesn't look like a global variable, because it's a local variable. The problem is, I can't call out a global function that's connected to the local variable, if I have on another lua script the following: local main = {}; if (main:GhostEntity(tr)) then Garry's Mod finds "GhostEntity" as a "method", not a function, if I don't include local main it simply finds it as an argument. How would I call a function like that? which features a local argument in it's name?
method is a function. You clearly don't understand what you are doing. No, that is not the problem, the problem is that you are creating an empty table and trying to call a method on it, but that empty table doesn't have any methods. You need to give that table you created a metatable, so that the table "knows" it has methods on it. Go and watch some OOP lua tutorials before you continue with this.
Hey, Thanks for the detailed answer, I have tried looking everywhere for explanations on how to set up MetaTables yet haven't quite understood much. I made some code I can safely show, that would explain the situtation I'm at quite well: Thing.lua: local main = {}; main.thing = {}; // stores all entity classes that should be ghosted on pickup main.thing["prop"] = true; main.GhostThing = true; main.AnotherThing = false; function main:Variables() return { GhostThing = self.GhostThing, AnotherThing = self.AnotherThing, }; end function main:GhostEntity(ent) --Code Here  end I'm still starting with Coding, that's why I'm having a bit of trouble, I'm trying to use the function "main:GhostEntity(ent)" in Thing.lua on another script. Would I need to use setmetatable( main:Variables, main ) ? What I'm confused about is why Tables with functions on them don't automatically add the functions to the Table to make it a "MetaTable" if that's how MetaTables work. Anyhow, any help to make me understand would be appreciated, or any "really good" tutorials can be good too, I'm just trying to figure out how I could use the function in other scripts.
Programming in Lua
Sorry, you need to Log In to post a reply to this thread.