• Urgent Help how to use Is Valid
    17 replies, posted
My scoreboard is causing a error. I am trying to make it display the level on the scoreboard but I first need to check if it's valid but I am confused how does IsValid work. self.cols[7]:SetText(ply.Level) -- Trying to check if this is valid before displaying it on the scoreboard [ERROR] lua/includes/util.lua:184: attempt to index local 'object' (a number value) 1. IsValid - lua/includes/util.lua:184 2. unknown - gamemodes/terrortown/gamemode/vgui/sb_row.lua:200 Timer Failed! [Simple][@gamemodes/terrortown/gamemode/vgui/sb_row.lua (line 209)] The text is updated using a timer every 15 seconds but when the error happens it continuously spam's console. Can someone please show me how to check if it's valid first.
Can you post the code you are using to check whether it is valid?
That error is caused by passing a normal data type through IsValid. I am working on fixing that.
[QUOTE=code_gs;45316975]That error is caused by passing a normal data type through IsValid. I am working on fixing that.[/QUOTE] I never knew you couldn't pass things like integers through IsValid.
Here's what I attempted I didn't understand how to use IsValid properly so don't bite. [CODE]local function loaddata() if ( IsValid( ply.Level ) ) then self.cols[7]:SetText(ply.Level) end self.cols[6]:SetText(ply:PS_GetPoints()) end timer.Simple( 15, loaddata)[/CODE]
Is Level a string? And if so, what would you count as it being "Invalid"?
[QUOTE=King Traitor;45317011]Here's what I attempted I didn't understand how to use IsValid properly so don't bite. [CODE]local function loaddata() if ( IsValid( ply.Level ) ) then self.cols[7]:SetText(ply.Level) end self.cols[6]:SetText(ply:PS_GetPoints()) end timer.Simple( 15, loaddata)[/CODE][/QUOTE] Why not use the TTT hook to add a scoreboard column instead? TTTScoreboardColumns And instead do [lua] if ply.Level then [/lua]
Level is a table it holds data for players level / exp gained. It would be considered invalid if it contained nothing. The problem occurs when a player hasn't played on the server before and the scoreboard goes to grab the ply.level only to find nothing. So basically when you spawn a bot.
[QUOTE=King Traitor;45317127]Level is a table it holds data for players level / exp gained. It would be considered invalid if it contained nothing. The problem occurs when a player hasn't played on the server before and the scoreboard goes to grab the ply.level only to find nothing. So basically when you spawn a bot.[/QUOTE] then use [lua] if #ply.Level == 0 then [/lua] The # operator functions like table.Count, so I'm pretty sure it'd return 0 for an empty table ( can't test now )
[QUOTE=HumbleTH;45317197]then use [lua] if #ply.Level == 0 then [/lua] The # operator functions like table.Count, so I'm pretty sure it'd return 0 for an empty table ( can't test now )[/QUOTE] Watch out, as the # operator only works for "array" (indexed) tables, whereas table.Count seems to work for both[code]print(#{ hello = "world" }) -- 0 print(#{ "hello world" }) -- 1 print(table.Count({ hello = "world"})) -- 1 print(table.Count({ "hello world" })) -- 1[/code]
[QUOTE=jackwilsdon;45316990]I never knew you couldn't pass things like integers through IsValid.[/QUOTE] Yeah, and because of IsValid's behaviour, if you pass false into it, it will return false.
but if a ply.Level doesn't get an initial value for new players, then trying to count it will give you other issues... So really depends on your implementation. [lua] if ply.Level then or if ply.Level != nil then or ply.Level = ply.Level or 0 [/lua]
I am still having no luck with this, I have tried your suggestions, but I get this error; attempt to index field 'cols' (a nil value) It works when using the default scoreboard: Code below [CODE]local function ShowLevel( pnl ) pnl:AddColumn( "Level", function( ply, label ) label:SetTextColor( Color(255,255,255,255) ) if ( ply.Level ) then return tostring(ply.Level) end return "1" end) end hook.Add( "TTTScoreboardColumns", "Level.TTTScoreboardColumns", ShowLevel )[/CODE] I have tried to create a similar function for the custom scoreboard that displays text in a column like this [CODE]self.cols[7]:SetText(ply.Level)[/CODE] but I have had no successes.
self.cols is nil. You need to set it to {} somewhere before you use it.
Probably calling the code from the wrong place. Where are you adding your code and what does the original code look like?
[QUOTE=Blasteh;45346744]Probably calling the code from the wrong place. Where are you adding your code and what does the original code look like?[/QUOTE] The clients scoreboard sb_row is calling the level{} table. The level table it's self is in an add-on on the server it has a client integration file. And what part of the original code do you need to see exactly the code I posted works for the default TTT scoreboard I am just trying to make it display the level for each player on the server on the custom scoreboard.
Well, I can see the default ttt scoreboard works because all the code for it is self-contained. You might be having issues with your custom scoreboard because the code is in the wrong place or it is being called incorrectly. Will probably need to see the code that specifically deals with how the scoreboard adds rows, and then your code that you are adding.
Are you trying to add a column to the TTT scoreboard? If so there is now a [URL=http://ttt.badking.net/guides/hooks]"TTTScoreboardColumns" hook[/URL], which allows custom columns to be added.
Sorry, you need to Log In to post a reply to this thread.