• attempt to call method 'Ping' (a nil value)
    8 replies, posted
I have created a scoreboard, which is throwing the above error when opened. The line it is referring to is this: [CODE]draw.SimpleText("Ping: "..client:Ping(),"scoreboard_header2font",w-200,39,TWDScoreboard.TextColor, TEXT_ALIGN_LEFT,TEXT_ALIGN_CENTER)[/CODE] (client being defined as LocalPlayer() above) If I open the file and save it AFTER loading into the game, it works fine. Thus, I am led to believe it is trying to fetch the players ping before the LocalPlayer() is valid. This will definitely be a simple fix, so any help would be greatly appreciated, however simple a mistake it may be!
You are correct in your assumption. You can use the [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/GM/InitPostEntity]GM:InitPostEntity[/url] hook to define [B]client[/B] after the player has joined the game completely (And LocalPlayer() won't return nil).
[QUOTE=JasonMan34;52464643]You are correct in your assumption. You can use the [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/GM/InitPostEntity]GM:InitPostEntity[/url] hook to define [B]client[/B] after the player has joined the game completely (And LocalPlayer() won't return nil).[/QUOTE] LocalPlayer() isn't returning nil, it is Ping that is nil. So, inside the hook should I do: [CODE]hook.Add( "InitPostEntity", "GetPing", function() client = LocalPlayer() end ) [/CODE] or should I do: [CODE]hook.Add( "InitPostEntity", "GetPing", function() GetPing = client:Ping() end ) [/CODE] and then use GetPing in the simple text rather than client:Ping()?
You should use the first code, just remember to localize [B]client[/B]. There's no reason to make it global
It was localized originally, but wouldn't that make it local to the hook? [editline]13th July 2017[/editline] [CODE] [ERROR] addons/twd_scoreboard/lua/autorun/client/cl_scoreboard.lua:37: attempt to index global 'client' (a nil value) 1. unknown - addons/twd_scoreboard/lua/autorun/client/cl_scoreboard.lua:37 [/CODE] I am now getting this error, after using the first hook I posted.
[QUOTE=SovereignNet;52464684]It was localized originally, but wouldn't that make it local to the hook? [editline]13th July 2017[/editline] [CODE] [ERROR] addons/twd_scoreboard/lua/autorun/client/cl_scoreboard.lua:37: attempt to index global 'client' (a nil value) 1. unknown - addons/twd_scoreboard/lua/autorun/client/cl_scoreboard.lua:37 [/CODE] I am now getting this error, after using the first hook I posted.[/QUOTE] Post your entire code. client isn't even defined
As you wish: [CODE]TWDScoreboard = {} --[CONFIG]-- TWDScoreboard.PrimaryColor = Color(20,20,20,254) TWDScoreboard.SecondaryColor = Color(53,149,181) TWDScoreboard.TextColor = Color(255,255,255) TWDScoreboard.Header = "The Walking Dead" TWDScoreboard.Footer = "Garry's Mod servers by www.sovereignnetworks.co.uk" --[END OF CONFIG]-- surface.CreateFont("scoreboard_headerfont",{font = "Bebas Neue", size = 57, shadow = true}) surface.CreateFont("scoreboard_header2font",{font = "Bebas Neue", size = 26, shadow = true}) surface.CreateFont("scoreboard_footerfont",{font = "Bebas Neue", size = 28, shadow = true}) surface.CreateFont("scoreboard_namefont",{font = "Bebas Neue", size = 32, shadow = true}) surface.CreateFont("scoreboard_infofont",{font = "Bebas Neue", size = 24, shadow = true}) function OpenScoreboard() hook.Add( "InitPostEntity", "GetPing", function() local client = LocalPlayer() end ) SBMainPanel = vgui.Create("DPanel") SBMainPanel:SetSize(700,ScrH()-150) SBMainPanel:SetPos(ScrW()/2-350,ScrH()/2-SBMainPanel:GetTall()/2) SBMainPanel:MakePopup(true) SBMainPanel.Paint = function(s,w,h) draw.RoundedBox(0,0,0,w,h,TWDScoreboard.PrimaryColor) draw.RoundedBox(0,0,h-35,w,35,TWDScoreboard.SecondaryColor) draw.RoundedBox(0,0,0,w,60,TWDScoreboard.SecondaryColor) draw.RoundedBox(0,0,55,w,5,Color(0,0,0,100)) draw.RoundedBox(0,0,h-5,w,5,Color(0,0,0,100)) draw.SimpleText(TWDScoreboard.Header,"scoreboard_headerfont",w/2,30,TWDScoreboard.TextColor, TEXT_ALIGN_CENTER,TEXT_ALIGN_CENTER) draw.SimpleText("Players: 1/32","scoreboard_header2font",w-200,21,TWDScoreboard.TextColor, TEXT_ALIGN_LEFT,TEXT_ALIGN_CENTER) draw.SimpleText("Ping: "..client:Ping(),"scoreboard_header2font",w-200,39,TWDScoreboard.TextColor, TEXT_ALIGN_LEFT,TEXT_ALIGN_CENTER) draw.SimpleText("Players: 1/32","scoreboard_header2font",200,21,TWDScoreboard.TextColor, TEXT_ALIGN_RIGHT,TEXT_ALIGN_CENTER) draw.SimpleText("Ping: "..client:Ping(),"scoreboard_header2font",200,39,TWDScoreboard.TextColor, TEXT_ALIGN_RIGHT,TEXT_ALIGN_CENTER) draw.SimpleText(TWDScoreboard.Footer,"scoreboard_footerfont",w/2,h-17.5,TWDScoreboard.TextColor, TEXT_ALIGN_CENTER,TEXT_ALIGN_CENTER) end local PlayerScrollPanel = vgui.Create("DScrollPanel", SBMainPanel) PlayerScrollPanel:SetSize(700,745) PlayerScrollPanel:SetPos(0,60) local PlayerList = vgui.Create("DListLayout", PlayerScrollPanel) PlayerList:SetSize(700,745 ) PlayerList:SetPos(0,0) PlayerList:SetVerticalScrollbarEnabled(false) if IsValid(SBMainPanel) then PlayerList:Clear() for k,v in pairs(player.GetAll()) do local PlayerPanel = vgui.Create("DPanel",PlayerList) PlayerPanel:SetSize(PlayerList:GetWide(),65) PlayerPanel:SetPos(0,0) PlayerPanel.Paint = function(s,w,h) draw.RoundedBox(0,0,0,w,h,Color(20,20,20,0)) draw.RoundedBox(0,0,60,w,2.5,Color(0,0,0,150)) draw.RoundedBox(0,0,62.5,w,2.5,Color(255,255,255,5)) draw.SimpleText(v:Nick(),"scoreboard_namefont",w/2,22,TWDScoreboard.TextColor,TEXT_ALIGN_CENTER,TEXT_ALIGN_CENTER) draw.SimpleText("Kills: "..v:Frags().."| Deaths: "..v:Deaths().."| Ping: "..v:Ping().."|Rank: Owner","scoreboard_infofont",w/2,45,TWDScoreboard.TextColor,TEXT_ALIGN_CENTER,TEXT_ALIGN_CENTER) end end end return true end function CloseScoreboard() SBMainPanel:Remove() end hook.Add("ScoreboardShow","OpenScoreboard",OpenScoreboard) hook.Add("ScoreboardHide","CloseScoreboard",CloseScoreboard)[/CODE]
[QUOTE=SovereignNet;52464704]hook.Add( "InitPostEntity", "GetPing", function() local client = LocalPlayer() end ) [/QUOTE] You need to define [B]local client[/B] outside the hook's function then do [B]client = LocalPlayer()[/B] inside the hook's function. You made it local to the function of the hook. Also take the "InitPostEntity" out of the OpenScoreboard function
-snip- [editline]13th July 2017[/editline] That has worked, thanks for the help.
Sorry, you need to Log In to post a reply to this thread.