• TTT Scoreboard
    7 replies, posted
I'm trying to set up a TTT server. I have ULX installed, and I am trying to get a column in the scoreboard that shows the rank of each player. To do this, I'm using the TTTScoreboardColumns hook. I used to get an error about ply in the getRankName function being nil, so I added the if statement to make sure it wasn't nil, but now I get another error. I also end up with a scoreboard that doesn't list anyone and doesn't disappear after you let go of tab. Any idea how to fix this? Anyone else even have the same problem? Code: [CODE]AddCSLuaFile(nil) hook.Add("TTTScoreboardColumns", "SetRankInScoreboard", TTTScoreBoardColumns) --Get the name of the rank to display in scoreboard function getRankName ( ply ) if (ply != nil) then rank = ply:GetUserGroup() if (rank == "superadmin") then return "Owner" elseif (rank == "admin") then return "Admin" elseif (rank == "operator") then return "Mod" elseif (rank == "regular") then return "Regular" elseif (rank == "user") then return "User" else return "" end end end --Adds rank column to scoreboard function TTTScoreBoardColumns(pnl) pnl:AddColumn("Rank", getRankName(ply)) end[/CODE] Error: [CODE] [ERROR] gamemodes/terrortown/gamemode/vgui/sb_row.lua:165: attempt to call field 'GetPlayerText' (a nil value) 1. UpdatePlayerData - gamemodes/terrortown/gamemode/vgui/sb_row.lua:165 2. SetPlayer - gamemodes/terrortown/gamemode/vgui/sb_row.lua:153 3. AddPlayerRow - gamemodes/terrortown/gamemode/vgui/sb_team.lua:97 4. UpdateScoreboard - gamemodes/terrortown/gamemode/vgui/sb_main.lua:278 5. Init - gamemodes/terrortown/gamemode/vgui/sb_main.lua:134 6. Create - lua/includes/extensions/client/panel/scriptedpanels.lua:153 7. ScoreboardCreate - gamemodes/terrortown/gamemode/cl_scoreboard.lua:27 8. unknown - gamemodes/terrortown/gamemode/cl_scoreboard.lua:34 [/CODE] Even when I use the code on [url]http://ttt.badking.net/guides/hooks[/url] it gives me the error. That code is (in the TTTScoreBoardColumns function) [CODE]pnl:AddColumn("ID", function(ply) return ply:UserID() end)[/CODE]
Anyone know how to fix this? I still haven't figured it out.
Take a look at this: [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/includes/file_only_works_on_autorefresh_possible_reasons.lua.html[/url] You're defining TTTScoreboardColumns function AFTER you hook.Add with a reference of the same name ( it'll be nil until auto-refresh )... As for the other error, not sure. As for optimizing the output... change: [code] --Get the name of the rank to display in scoreboard function getRankName ( ply ) if (ply != nil) then rank = ply:GetUserGroup() if (rank == "superadmin") then return "Owner" elseif (rank == "admin") then return "Admin" elseif (rank == "operator") then return "Mod" elseif (rank == "regular") then return "Regular" elseif (rank == "user") then return "User" else return "" end end end[/code] to: [code]local RankNames = { superadmin = "Owner"; admin = "Admin"; operator = "Mod"; regular = "Regular"; user = "User"; default = ""; }; --Get the name of the rank to display in scoreboard function getRankName ( _p ) if ( !IsValid( _p ) ) then return RankNames.default; end // Micro-caching because NW Vars can cause lag... if ( !_p.__GroupCache || CurTime( ) - _p.__GroupCache > 3 ) then // Update the cache timer.. _p.__GroupCache = CurTime( ); // Grab the UserGroup local _group = _p:GetNWString( "UserGroup" ); // Grab the rank-name from the table. If it doesn't exist use the default... _p.__GroupName = RankNames[ _group ] || RankNames.default; end // Always return _p.__GroupName because it'll always be set and cached ( unless someone changes it somewhere else ) return _p.__GroupName; end[/code]
Thanks. I'll try this when I get back home today. Edit: Not that I expected it to since you said you don't know about it, but it didn't work.
I haven't messed with the new column system but make sure you're running the latest version. The code I provided is an optimized version of what you posted ( -snip didn't read your code properly- and then caches to prevent asking too many times ).. Now, for fixing your error... [url]https://github.com/garrynewman/garrysmod/blob/master/garrysmod/gamemodes/terrortown/gamemode/vgui/sb_row.lua#line-57-65[/url] AddColumn expects a function. You're providing a result. To pass a function as a function ( reference ) remove the parens... Here's the fixed and optimized code: [code] local RankNames = { superadmin = "Owner"; admin = "Admin"; operator = "Mod"; regular = "Regular"; user = "User"; default = ""; bot = "NPC"; }; --Get the name of the rank to display in scoreboard local function getRankName( _p ) if ( !IsValid( _p ) ) then return RankNames.default; end if ( _p:IsNPC( ) || _p:IsBot( ) ) then return RankNames.bot; end // Micro-caching because NW Vars can cause lag... if ( !_p.__GroupCache || CurTime( ) - _p.__GroupCache > 3 ) then // Update the cache timer.. _p.__GroupCache = CurTime( ); // Grab the UserGroup local _group = _p:GetNWString( "UserGroup" ); // Grab the rank-name from the table. If it doesn't exist use the default... _p.__GroupName = RankNames[ _group ] || RankNames.default; end // Always return _p.__GroupName because it'll always be set and cached ( unless someone changes it somewhere else ) return _p.__GroupName; end --Adds rank column to scoreboard hook.Add( "TTTScoreboardColumns", "SetRankInScoreboard", function( pnl ) pnl:AddColumn( "Rank", getRankName ); end );[/code] Your errors and other notes are: [B]1)[/B] You used hook.Add to add a function ( 3rd arg ) that gets defined [B]later [/B]in the file ( meaning it'd only work if the game-mode was auto-refreshed ). One of the reasons I prefer adding the function in the hook instead of a function reference is to avoid issues like this, and do avoid overwriting an existing function name ( even if it is a local reference, if a function is the same name then that function won't be available in the file ). I will occasionally add a function reference iff ( if and only if ) it serves the purpose of reducing code, ie the function is used in other places ). [B]2)[/B] TTTScoreboardColumns _panel.AddColumn expects a [B]function reference[/B] much like hook.Add, however you called the function providing a string instead. [B]3)[/B] ply != nil could have issues. Use IsValid( ply ) instead because it'll make sure the reference is valid in every sense... After IsValid( ply ) you could also add && ply:IsPlayer( ). Another note on this is that you can use inverse logic ( &&s become ||s, ||s become &&s, < becomes >=, >= becomes <, <= becomes >, > becomes <=, ! becomes !! or nothing, etc... ) and avoid tabbing inward ( Inverse logic is used in standard Lua because continue doesn't exist in loops except in GLua ). [B]4)[/B] Instead of returning nothing when the player is nil ( !IsValid ), error nicely ( just in case a string IS required which it may not be ) and return a default value. I denoted it as default = ""; ie empty string, but something. [B]5)[/B] Bots can also be part of the scoreboard; I added a check for IsNPC or IsBot ( IsValid isn't needed because we know the player reference is valid because of the return if !IsValid ) and denoted it as bot = "NPC"; I think that's all.... Hopefully this solves your problem and gives some additional insight!
Thanks, I'll have to test it later. If you haven't already noticed, im new to Lua :P
That's great news that you're learning Lua! It is a fun language, especially GLua; it's nice to be able to see changes to code occurring right in front of you without needing to recompile binaries, execute, etc... and logic is universal so when you write in other languages, if you know them, then it is easy to transcribe logic from one language to another. I'd recommend looking into BNF / EBNF ( it describes the language through a series of keywords and replacements which can contain keywords; if you see a word that isn't on the left ( like "Name" then it is defined elsewhere but typically easy to decipher ), | means or, [ ] is optional, '=' etc.. is literal. Scroll to bottom: [url]http://www.lua.org/manual/5.1/manual.html[/url] Anyway, here's a ton of data. I'd recommend looking at Useful Programs ( Wamp, Autosizer, Process Explorer and Desktops ), Setting up a Local SRCDS if you haven't already, potentially adding me on Steam if you're interested, and if you're looking for a dev-platform check out my dev-base ( constantly adding to it; provides building blocks for creating game-mode / addons ). I'll be releasing it as a Redistributable Addon very soon so any addons that use the resources can require it, and some of the features will also be released standalone too ( networking system is released by the file-structure is bad because it uses default addon format. Going to change my autoloader to work on addons and add some optimizations, then those will be released ) If you're anywhere ranging from new to Lua / Developing to Expert, I highly recommend using a LOCAL SRCDS to develop your work on instead of starting a listen server ( Single-player / multiplayer game "Server" started from within Garry's Mod CLIENT Game ) because the end result ( "work" being ran on dedicated server ) may vary or not work if developed on a listen-server. Here's how to set up a local SRCDS ( not all of these are required, just like you don't need to mount all games; but do force-directory as mount.cfg doesn't like the names of the games when they're left to install in other directories ): Setting up a Local Dedicated Server is very easy. Step by step instructions here: [url=https://dl.dropboxusercontent.com/u/26074909/tutoring/server_srcds_steamcmd/setting_up_a_server_with_steamcmd.lua.html]Setting up a Local Development SRCDS[/url] You may consider setting up a [url=https://dl.dropboxusercontent.com/u/26074909/tutoring/server_srcds_steamcmd/fastdl_setup_instructions.lua.html ]Local FastDL Server[/url] ( need local Web-Server; I recommend using WAMP, link in Useful Programs ) too! [url=https://dl.dropboxusercontent.com/u/26074909/tutoring/server_srcds_steamcmd/setting_up_downloads_using_recursive_resource_system.lua.html ]Recursive Resource Loader[/url] [url=https://dl.dropboxusercontent.com/u/26074909/tutoring/___welcome_docs/_welcome_useful_programs_and_notepadpp_upgrades.lua.html]Useful Programs[/url] I have written over 400 tutorials and completed "systems" in Lua for Garry's Mod. I tutor for negotiable rates / free / donations, and answer questions on on one for free / donations; feel free to add me on Steam if you need some guidance. Here are some resources to help you get started: ------------------------------------------------------------- Generalized Lua Help ( Links to Wikis, Answers the question of "Where do I post a simple question or DarkRP Specific question", links to other resources compiled by forum members ) [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/___welcome_docs/_welcome_general_lua_learning.lua.html[/url] Useful Programs ( SteamCMD, Autosizer, Desktops, Process Explorer ) and Notepad++ Upgrades [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/___welcome_docs/_welcome_useful_programs_and_notepadpp_upgrades.lua.html[/url] If you're looking for an "empty" game-mode to start developing on ( for creating addons in an environment meant for dev work, developing a new game-mode, etc... ) which has a lot of building-blocks to make development easier, and dev-tools for debugging, etc.. I recommend running my dev-base ( supports smart-autorefresh for Linux or Windows with -disableluarefresh ) AcecoolDev_Base Skeletonized Game-Mode ( Never worry about Include or AddCSLuaFile ever again; comes with New Hooks, Console Commands, Meta-Table Objects, Helper Functions, Extended Functionality, and more! ) AcecoolDev_Networking: Only has the networking and data system with required files. Also has UMSG to NetMSG Conversion Mapper and will eventually include NW/DT/etc.. Var map through networking system... AcecoolDev_Base Addon - All of the features above, but packaged as an addon. Find out more and Download here: [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/___welcome_docs/_welcome_acecooldev_base_gamemode_info.lua.html[/url]
Thanks for the help (Yes, it does work), and I already have an SRCDS server :)
Sorry, you need to Log In to post a reply to this thread.