Lua help


// id = Item ID(example : paper)
// We need to add "Stock" to the function because the function returns the number of the current stock of the item.
print(GAMEMODE:paperStock()) // Working(returns 40 (pre set value))
print(GAMEMODE:..id.."Stock"..()) // does not working, it should call the function with through variable

Use id()

But I need to add the “Stock”

[lua]
print( GAMEMODE:paperStock() … " Stock" )
[/lua]

Also your last comment, I don’t know where you got that idea from. The only time you would need to do anything vaguely similar to what you are suggesting in that code is when you need to call a function in a table with a variable member name eg MyTablevariable … “constant”

[lua]GAMEMODEid…“Stock”[/lua]is same as[lua]GAMEMODE:paperStock()[/lua] if id is ‘paper’

Thanks, but there is problem that it returns


40 nil	nil	nil	nil	nil	nil	nil	nil


I want it to return only 40.



( GAMEMODE:paperStock() )


Or



local blah = GAMEMODE:paperStock()


Going off of what you said above, simply calling it only printed 40. E.g.



print(GAMEMODE:paperStock()) -- 40


What @Ia_grib posted would return the exact same unless you’re doing something different.

If they’re both returning all of that and you just want get the first argument, wrap the function call in parentheses, e.g.



(foo())


It’ll reduce the return set to only the first value.

[editline]6th November 2017[/editline]

ninja’d

I am using


GAMEMODE[id.."Stock"](GAMEMODE)

That’s a function call, as said wrap it in parentheses.



(GAMEMODE[id.."Stock"](GAMEMODE))


Whole code :




for _, v in pairs( GM.Items ) do

	GM["Set"..ITEM.ID.."Stock"] = function( self, val )
		
		if( CLIENT ) then return end

		_G["SetGlobalFloat"]( ITEM.ID.."Stock", val );
		
	end
	
	GM[ITEM.ID.."Stock"] = function( self )

			
		local f = _G["GetGlobalFloat"]( ITEM.ID .."Stock", 100 );
		
		if( f == false ) then
			
			return false;
			
		end
		
		return f or 100;
		
	end



end

if SERVER then

local number = (GAMEMODE["waterStock"](GAMEMODE))
GAMEMODE["SetwaterStock"](GAMEMODE, number - 1)

print((GAMEMODE["waterStock"](GAMEMODE)))
print(GAMEMODE:paperStock())

end



Problem : Why the “Set” function also reduce the number by 1 for the “paperStock” function and for all items.

BUMP

Why do you use ITEM.ID there? Since you’re iterating through the GM.Items table, wouldn’t you want to do anything with the ‘v’ variable?

You are right, I have changed it to ‘v.ID’ .

But still this :



local number = (GAMEMODE[id.."Stock"](GAMEMODE)) // id == water
GAMEMODE["Set"..id.."Stock"](GAMEMODE, number - 5) 
print((GAMEMODE[id.."Stock"](GAMEMODE))) // id == water, returns 15
print((GAMEMODE["paperStock"](GAMEMODE))) // returns 15 (Even that I only reduced the water stock)


You should try to print out different values and variables before and after executing this code for debugging reasons. I. e. check if paper stock wasn’t 15 from the beginning, or if id isn’t paper instead of water.

I have already checked, it reduces for all.