Is this a proper way to setup a concommand?

function addToTable(player, unID)
if (player:IsSuperAdmin())
LoadTablesOnStart()
for k, v in pairs(donators) do
if (unID == v)
print(“This ID was already found in the table”)
break
end
end
donators.Insert(unID)
SaveTables()
end
end
concommand.Add( “adddonator”, addToTable)

Will this work? If not, what is wrong with it? I dont really understand how to set up a function such that it will work with a concommand

[editline]08:56PM[/editline]

LoadTablesOnStart and SaveTables are functions defined elsewhere in the code

[editline]09:07PM[/editline]

just realized how bad this looks

You dont have to use pastebin you can but your code in lua tags. anyway here is the wiki page on concommands http://wiki.garrysmod.com/?title=Concommand.Add and the proper fomrmat is like how you have it.
concommand.Add(“CommandName”,FunctionName) these can be the same.Also as a style tip use bump case for the command if its more then one word. So for yours instead of adddonator it could be AddDonator. This is up to you though it helps when your reading your code.



function LoadTablesOnStart()
	if(file.Exists("data/donators.txt")) then
		local readstring = file.Read("data/donators.txt")
		local donators = util.KeyValuesToTable(readstring)
	end
 end
 function SaveTables()
	local savestring = util.TableToKeyValues(donators)
	file.Write("data/donators.txt",savestring)
end


function addToTable(player, unID)
if (player:IsSuperAdmin())
LoadTablesOnStart()
for k, v in pairs(donators) do
	if (unID == v)
	print("This ID was already found in the table")
	break
	end
	end
	donators.Insert(unID)
	print("ID added")
	SaveTables()
end
end
concommand.Add( "adddonator", addToTable)	

function IsDonator(ply)
LoadTablesOnStart()
	for k,v in pairs(donators) do
		if (ply:SteamID() == v)
			return true
		end
	end
	return false
end


Just loaded this up. Why doesnt it work?

Functions in the “file” library work relative to the data directory. Thus, it would simply be file.Write(“donators.txt”) or file.Exists(“donators.txt”), unless you really want a folder named “data” within your “data” directory.

Thanks for the tip. Ill make that change right now. But when I type “adddonator whateversteamid” in console, it says the command doesnt exist

I think your function should be formatted something like this…



function addToTable(player, command, args)
local unID = args[1]


And are you sure where you have this concommand it is getting loaded? Just under concommand.Add() put print(“CONCOMMAND LOADED”) and then check the console to make sure it’s getting added.

You need a “then” at the end of line 2

You are also looping through a table ‘donators’ which is only declared locally in ‘LoadTablesOnStart()’.
Either make the table global, or return the table in LoadTablesOnStart() like this:

[lua]
function LoadTablesOnStart()
local donators = {}
if(file.Exists(“data/donators.txt”)) then
local readstring = file.Read(“data/donators.txt”)
donators = util.KeyValuesToTable(readstring)
end

return donators

end
[/lua]

Then you can use the table in your other function:

[lua]
local donators = LoadTablesOnStart()
[/lua]

If you’d read the wiki, you’d realise concommand.Add passes three arguments to the callback function - player , command and a table of arguments.

Thats what I was wondering FlapJack…hmmmmm…ok