• Running a function inside a variable?
    9 replies, posted
I've been making a gamemode and I wanted it to be customizable. I'm having some trouble with my custom function. Here is one of my custom functions. GLOBAL.CreateTeam({ Name = "Test", PlayerDeath = function(ply, weapon, killer) ply:ChatPrint("You died") end, PlayerSpawn = function(ply) ply:ChatPrint("Respawned") end, CanPlayerSuicide = function(ply) return false end }) But for some reason PlayerDeath, PlayerSpawn, and CanPlayerSuicide doesn't work. Here is one of the GM functions: function GM:PlayerSpawn(ply) if GLOBAL.Teams[team.GetName(ply:Team())] then local tbl = GLOBAL.Teams[team.GetName(ply:Team())] tbl.PlayerSpawn() end end
tbl.PlayerSpawn(ply) ( You forgot to put the player in the function )
Doesn't seem to work. There are no script errors, but if I add in an identifier to my function it gives out an error stating that it needs a parenthesis for some reason.
Then it could probably be that the if statement is returning false. Try putting out some prints and such to see what lines are executed. -- if GLOBAL.Teams[team.GetName(ply:Team())] then There is an extra parenthesis at the "ply:Team())"
They are running perfectly fine, the function isn't working tho.
Could you post the full error message? "Also, what's the difference between an if statement with parentheses and an if statement that has none?" The code becomes more clear. Easier to read.
Well, there is no error, I just get an error message if I modify the function: function(ply) => function test(ply) [ERROR] addons/test/gamemodes/test/gamemode/init.lua:26: '(' expected near 'test' That's the error that pops up if I add in the name.
Then don´t do that. Having functions in tables should be like this: func = { test = function( ply ) print(ply:Nick(), ply:Health() end novar = function() print(" no var in this func " ) end } -- And calling the function local bob = FindMetaTable("Player") func.test( bob ) -- Calling the function with no variables in it func.novar()
I was actually doing that already but there were problems with making my table, instead of doing this: tbl = { var = "Hello!" } I did this: tbl = {} tbl.var = "Hello!"
look into the darkrp jobs.lua you have to make a table and call the table as a function like darkrp does with the jobs.lua with the playerspawn = thing
Sorry, you need to Log In to post a reply to this thread.