• attempt to call method 'IsUserGroup' (a nil value)
    7 replies, posted
Trying to make HUD. Im still a beginner in lua.. but it seems like this only happens if i put the lua file into autorun/client. It works on lua_openscript though. Here is the code: function hud() -- Consider functions like a cyber button, and everything INSIDE the function turns on when this cyber button is pressed. local health = LocalPlayer():Health() draw.RoundedBox(0, 1430, ScrH() - 850, health, 17, Color(255,20,147)) end   if ply:IsUserGroup("superadmin") then  ply:ChatPrint("ID: Kokichi Ouma\n Abilities: Rage button,Hammer") hook.Add("HUDPaint", "MyHudName", hud) -- I'll explain hooks and functions in a second local mat = Material( "materials/hud.png" ) hook.Add( "HUDPaint", "DrawTexturedRectUV_example1", function() surface.SetDrawColor( color_white ) surface.SetMaterial( mat )      surface.DrawTexturedRect( 10, 100, 1700, 800 ) end ) end   if ply:IsUserGroup("superadmin") then hook.Add("HUDPaint", "HUDKokichi", hud) -- I'll explain hooks and functions in a second local kok = Material( "materials/oma.png" ) hook.Add( "HUDPaint", "DrawTexturedRectUV_example2", function() surface.SetDrawColor( color_white ) surface.SetMaterial( kok )      surface.DrawTexturedRect( 0,800, 500, 120 ) end ) end  if ply:IsUserGroup("superadmin") then local nick = Entity(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18):Nick() hook.Add("HUDPaint" , "DrawMyHud" , function() draw.SimpleText("Name: "..nick, "What",50,260,Color(139,0,0)) draw.SimpleText("Ultimate Supreme Leader", "supreme",120,840,Color(255,255,0)) end) end
You have to remember that this is a clientside file. You should use LocalPlayer() instead of ply if I am reading this correctly. If you are trying to do this for other players, loop through all the players and check their user group from there.
Hmm What if i already did local ply = LocalPlayer()
inside the hook callback function local ply = LocalPlayer() if not IsValid(ply) then return end -- validate the object before using it
This is the best approach for this problem. The autorun/client directory sometimes gets fired before there's actually a localplayer (for the same reason MOTDs on servers sometimes open whilst you're still on the "Sending client info...." screen). Lua in Garry's Mod is sometimes weird, which means you should always validate your shit. This is also a very good practice to learn, as one should never trust the user, for they're evil, dumb and often 13 years old.
LocalPlayer() wiki. LocalPlayer() will return NULL, until all entities have been initialized. See GM:InitPostEntity. When you first run your script, entities like the player doesn't exist.
So just do a check.
Guys i fixed it, need to put the declaration in the function to make it work. Thanks ! function hud() local client = LocalPlayer() local health = client:Health()        surface.SetDrawColor( color_white ) surface.SetMaterial( mat ) surface.DrawTexturedRect(10, 100, 1700, 800) if client:IsUserGroup("Ultimate Supreme Leader") then surface.SetDrawColor( color_white ) surface.SetMaterial( kok ) surface.DrawTexturedRect(0, 800, 500, 120) local nick = client:Nick() draw.SimpleText("Name: "..nick, "What", 50, 260, Color(139,0,0)) draw.SimpleText("Ultimate Supreme Leader", "supreme", 120, 840, Color(255,255,0))     draw.RoundedBox(0, 1430, ScrH() - 850, health, 17, Color(255,20,147))     end end hook.Add("HUDPaint", "HUDPaintIdentifier", hud)
Sorry, you need to Log In to post a reply to this thread.