Reputation

I am new to lua, so I was wondering if anyone could point out any obvious errors I’ve made in this script I’ve been working on.


local CATEGORY_NAME = "Utility"

CreateConVar("ulx_up", 3, FCVAR_ARCHIVE, "Adding reputation to a player.")
CreateConVar("ulx_down", 30, FCVAR_ARCHIVE, "Removing reputation from a player.")
CreateConVar("ulx_rep", 0, FCVAR_ARCHIVE, "Check your current reputation.")
CreateConVar("ulx_reptime", 30, FCVAR_ARCHIVE, "How long before the server adds reputation to a player")

if !file.Exists( "ulx", "DATA" ) then
	file.CreateDir( "ulx" )
end

if !file.Exists( "ulx/Reputation", "DATA" ) then
	file.CreateDir( "ulx/Reputation" )
end

--[[
	ulx.up( calling_ply, target_ply )
	calling_ply	: PlayerObject	: Player who ran the command.
	target_ply	: PlayerObject	: Player who is receiving reputation.
	
	This function is the ULX function that allows for a reputation of a player.
]]
function ulx.up( calling_ply, target_ply )
	if reason and reason ~= "" then
		ulx.fancyLogAdmin( calling_ply, "#T reputation increased to rep_count (#s)", target_ply )
	else
		reason = nil
		ulx.fancyLogAdmin( calling_ply, "#A added reputation to #T", target_ply )
	end
	ulx.up( target_ply, calling_ply, reason )
end
local rep = ulx.command( CATEGORY_NAME, "ulx up", ulx.up, "!up" )
rep:addParam{ type=ULib.cmds.PlayerArg }
rep:addParam{ type=ULib.cmds.StringArg, hint="Add Reputation To This Player", ULib.cmds.optional, ULib.cmds.takeRestOfLine }
rep:defaultAccess( ULib.ACCESS_ADMIN )
rep:help( "Add Reputation to a player." )

--[[
	ulx.up( target_ply, calling_ply, reason )
	target_ply	: PlayerObject	: Player who receives reputation.
	calling_ply	: PlayerObject	: Admin or player who added the reputation
	rep_count	: PlayerObject	: Reputation Received
	
	This helper function is what adds the reputation to the player's table and calls the save helper function.
	
]]
function ulx.up( target_ply, calling_ply, reason )

	if target_ply.reptable == nil then
		target_ply.reptable = {}
	end
	
	if target_ply.reptable["repcount"] == nil then
		target_ply.reptable["repcount"] = 0
	end
	
	if target_ply.reptable["Reputation"] == nil then
		target_ply.reptable["Reputation"] = {}
	end
	
	table.insert(target_ply.reptable["Reputation"], {os.date(), calling_ply:Nick(), rep})
	
	target_ply.reptable["repcount"] = target_ply.reptable["repcount"] + 1
	ULib.tsayColor(target_ply, Color(0,0,0,255), "Rep: " , Color(255,255,255,255), "Your reputation was increased by ", Color(0,0,0,255), "(", Color(0,255,0,255), calling_ply:Nick(), Color(0,0,0,255), ")", Color(255,255,255,255), " ", Color(255,0,0,255), reason)
	


	if target_ply.reptable["repcount"] >= GetConVarNumber( "ulx_up" ) then
		if GetConVarNumber( "ulx_rep" ) == 0 then
			target_ply.reptable["repcount"] = target_ply.reptable["repcount"] - 1
			ulx.RepSave( target_ply )
		else
			local btime = tostring( GetConVarNumber( "ulx_reptime" ) )
			target_ply.reptable["repcount"] = target_ply.reptable["repcount"] - 1
			ulx.RepSave( target_ply )
		end
	else
		ulx.RepSave( target_ply )
	end
	

end

--[[
	ulx.DecayRep()
	
	This function runs on a timer and adds 1 reputation to a player. The player needs to be playing on the server
	when this is called to receive reputation.
]]
function ulx.DecayRep()
	print("Reputation Timer Running")

	for _, pl in pairs ( player.GetAll() ) do
	
		if pl.reptable == nil then continue end
		if pl.reptable["repcount"] == nil then continue end
		if pl.reptable["repcount"] <= 0 then continue end
		
		pl.reptable["repcount"] = pl.reptable["repcount"] + 1
		ulx.RepSave( pl )
		
		ULib.tsayColor(pl, Color(0,0,0,255), "Rep: " , Color(255,255,255,255), "Your reputation has been increased for playing. It has been raised by ", Color(255,0,0,255), "1")
	
	end
	
	timer.Create( "ULX_DecayTimer", GetConVarNumber( "ulx_down" ) * 60, 1, ulx.DecayRep )
end
timer.Create( "ULX_DecayTimer", 1, 1, ulx.DecayRep )

--[[
	ulx.RepSave( pl )
	pl	: PlayerObject	: Player whos reputations are being saved
	
	This helper function saves the player's reputations to a text file for future use.
]]
function ulx.RepSave( pl )

	local tbl = pl.reptable
	local SID = pl:SteamID64()
	
	toencode = util.TableToJSON(tbl)

	file.Write("ulx/Reputation/"..SID..".txt", toencode)

end

--[[
	ulx.RepLoad( pl )
	pl	: PlayerObject	: Player whos reputations are being loaded
	
	This helper function loads a player's saved reputationss from their file to their player object.
]]
function ulx.RepLoad( pl )

	local SID = pl:SteamID64()
	if file.Exists( "ulx/Reputation/" .. SID .. ".txt", "DATA" ) then
		local todecode = file.Read( "ulx/Reputation/" .. SID .. ".txt", "DATA" )
		
		local tbl = util.JSONToTable( todecode )
		pl.reptable = tbl
	
	end

end
hook.Add( "PlayerAuthed", "RepLoad", ulx.RepLoad )

--[[
	ulx.rep( calling_ply, target_ply )
	calling_ply	: PlayerObject	: Admin or player who runs the command.
	target_ply	: PlayerObject	: Target player whos reputations are being displayed.
	
	This function allows an admin or whoever is granted access to see the history of reputations on a target player.
]]
function ulx.rep( calling_ply, target_ply )
	
	if not IsValid(calling_ply) then return end
	if not IsValid(target_ply) then return end
	
	if target_ply.reptable == nil then
		target_ply.reptable = {}
	end
	
	if target_ply.reptable["Reputation"] == nil then
		ULib.console( calling_ply, "Showing reputation for player: " .. target_ply:Nick() )
		ULib.console( calling_ply, "This players reputation is currently zero." )
	else
		ULib.console( calling_ply, "Showing reputation for player: " .. target_ply:Nick() )
		ULib.console( calling_ply, "Date                     By" )
		ULib.console( calling_ply, "---------------------------" )
		for k, v in pairs( target_ply.reptable[ "Reputation" ] ) do
			local date = v[1]
			local by = v[2]
			line = date .. string.rep(" ", 25 - date:len()) .. by .. string.rep(" ", 35 - by:len()) .. reason
			ULib.console( calling_ply, line )
		end
	end	
end
local checkrep = ulx.command( CATEGORY_NAME, "ulx checkrep", ulx.checkrep )
checkrep:addParam{ type=ULib.cmds.PlayerArg }
checkrep:defaultAccess( ULib.ACCESS_ADMIN )
checkrep:help( "Lists all reputations to console." )

--[[
	ulx.down( calling_ply, target_ply, rep_count )
	calling_ply		: PlayerObject	: Admin or player who runs the command.
	target_ply		: PlayerObject	: Target player whos reputations are being displayed.
	rep_count	: Integer		: Amount of reputation to remove from player.
	
	This function will allow an admin to remove active reputations from a target player.
]]
function ulx.down( calling_ply, target_ply, rep_count )
	
	if not IsValid(calling_ply) then return end
	if not IsValid(target_ply) then return end
	
	if target_ply.reptable == nil then
		target_ply.reptable = {}
	end
	
	if target_ply.reptable["repcount"] == nil then
		ULib.console( calling_ply, "Player " .. target_ply:Nick() .. " has no reputation points.")
		return
	end
	
	if target_ply.reptable["repcount"] == 0 then
		ULib.console( calling_ply, "Player " .. target_ply:Nick() .. " has no reputation points.")
		return
	end
		
	local total_reputation = target_ply.reptable["repcount"]
	local to_remove = rep_count
	
	if to_remove > total_reputation then
		to_remove = total_reputation
	end
	
	target_ply.reptable["repcount"] = total_reputation - to_remove
	ulx.fancyLogAdmin( calling_ply, "#S removed removed reputation from #T.", target_ply )
	ULib.console( calling_ply, "You removed (" .. to_remove .. ") reputation from " .. target_ply:Nick() .. ". Player current has (" .. target_ply.reptable["repcount"] .. ") reputation points.")

end
local down = ulx.command( CATEGORY_NAME, "ulx down", ulx.Down )
down:addParam{ type=ULib.cmds.PlayerArg }
down:addParam{ type=ULib.cmds.NumArg, hint="reputation to remove" }
down:defaultAccess( ULib.ACCESS_ADMIN )
down:help( "Removes reputation from a player." )

--[[
	ulx.RemoveReputationHistory( calling_ply, target_ply )
	calling_ply		: PlayerObject	: Admin or player who runs the command.
	target_ply		: PlayerObject	: Target player whos reputations are being removed.
	
	This function removes all reputation from a player.
]]
function ulx.RemoveReputationHistory( calling_ply, target_ply )
	if not IsValid(calling_ply) then return end
	if not IsValid(target_ply) then return end
	
	local SID = target_ply:SteamID64()
	
	if file.Exists( "ulx/Reputation/"..SID..".txt", "DATA" ) then
		file.Delete( "ulx/Reputation/"..SID..".txt" )
		target_ply.reptable = nil
		ULib.console( calling_ply, "You removed all reputation records for player: " .. target_ply:Nick() .. "." )
		ulx.fancyLogAdmin( calling_ply, "#A removed all reputation records for player #T.", target_ply )
	else
		ULib.console( calling_ply, "Unable to find any reputation records for player: " .. target_ply:Nick() .. "." )
	end
		

end
local down = ulx.command( CATEGORY_NAME, "ulx down", ulx.RemoveReputation )
down:addParam{ type=ULib.cmds.PlayerArg }
down:defaultAccess( ULib.ACCESS_SUPERADMIN )
down:help( "Remove Reputation From A Player." )

--[[
	ulx.ListAllReputation( calling_ply )
	calling_ply		: PlayerObject	: Admin or player who runs the command.
	
	Shows a list of all players on the server and how many total reputations and active reputations they have.
]]
function ulx.ListAllReputation( calling_ply )

	ULib.console( calling_ply, "Showing total reputation for all players:" )
	ULib.console( calling_ply, "Player Name                          Reputation" )
	ULib.console( calling_ply, string.rep( "-", 75 ) )
	ULib.console( calling_ply, rep_count)
	
	for _, pl in pairs( player.GetAll() ) do
		if pl.reptable == nil then continue end
		if pl.reptable[ "repcount" ] == nil then continue end
		local totalrep = tostring( table.Count( pl.reptable[ "Reputation" ] ) )
		local activerep = tostring( pl.reptable[ "repcount" ] )
		ULib.console( calling_ply, pl:Nick() .. string.rep(" ", 37 - pl:Nick():len()) .. totalrep .. string.rep(" ", 23 - totalrep:len()) .. activerep )
	end
	
end
local listrep = ulx.command( CATEGORY_NAME, "ulx listrep", ulx.ListAllReputation )
listrep:defaultAccess( ULib.ACCESS_ADMIN )
listrep:help( "Returns a list of all connected players and their reputation." )

Did you test it?
If so, what is the error?

This is ULX AWarn with any occurrence of the word ‘warn’ being changed to ‘reputation’, not yours.

I didn’t say it was mine, and I was suggested to use this to make a reputation system to make it, so I did

Also, I don’t get any errors, but it doesn’t print the reputations.