• I don't know what I am doing. Table help and Nil values.
    3 replies, posted
So im trying to set up my hud EXACTLY to my liking but alas while the code works in my head, it does not work on paper... What i am trying to do is set up a HUD element that tells you what your current weapon is and if you are NOT holding a weapon then to return nothing (Without errors) Also, i am trying to make a table that contains the names of the possible weapon_* choices any regular person can have on my server, then display the weapon name in text as i want it to be. However the table i have written returns errors, and I am unsure as how to utilize my table even if it didn't give me errors. As for my " if WEAP:IsValid() then" this also returns errors. **See bottom of post for error messages. Here is my code as i have it. This code does not work and returns errors. [CODE] -----------Weapons Table---------------- myweapons={} weapon_crowbar[1]="Crowbar" weapon_fists[2]="Fists" weapon_pistol[3]="Pistol" weapon_357[4]="357" weapon_smg1[5]="SMG" weapon_ar2[6]="Pulse Rifle" weapon_shotgun[7]="Shotgun" weapon_crossbow[8]="Crossbow" weapon_frag[9]="Grenade" weapon_rpg[10]="RPG" weapon_slam[11]="Slam" ----------------------------------------- surface.CreateFont("s1", { --s1 becomes the custom name of font size = 25, weight = 700, antialias = true, shadow = false, font = "ScoreboardText"}) local WEAP = LocalPlayer():GetActiveWeapon():GetClass() surface.SetTextColor( 255, 255, 255, 255) surface.SetTextPos( 10, 80 ) surface.SetFont( "s1" ) if WEAP:IsValid() then surface.DrawText( "Weapon: "..WEAP) else surface.DrawText( "Weapon: None") end end [/CODE] EDIT: The Errors are [ERROR] gamemodes/gxm/gamemode/cl_init.lua:10: attempt to index global 'weapon_crowbar' (a nil value) 1. unknown - gamemodes/gxm/gamemode/cl_init.lua:10 [ERROR] gamemodes/gxm/gamemode/cl_init.lua:96: Tried to use a NULL entity! 1. GetClass - [C]:-1 2. unknown - gamemodes/gxm/gamemode/cl_init.lua:96
Doesn't work that way. Instead of weapon_rpg and so on you need to do myweapons[number]
[code] -----------Weapons Table---------------- local myweapons = {} myweapons["weapon_crowbar"]="Crowbar" myweapons["weapon_fists"]="Fists" myweapons["weapon_pistol"]="Pistol" myweapons["weapon_357"]="357" myweapons["weapon_smg1"]="SMG" myweapons["weapon_ar2"]="Pulse Rifle" myweapons["weapon_shotgun"]="Shotgun" myweapons["weapon_crossbow"]="Crossbow" myweapons["weapon_frag"]="Grenade" myweapons["weapon_rpg"]="RPG" myweapons["weapon_slam"]="Slam" ----------------------------------------- surface.CreateFont("s1", { --s1 becomes the custom name of font size = 25, weight = 700, antialias = true, shadow = false, font = "ScoreboardText"}) local WEAP = LocalPlayer():GetActiveWeapon() surface.SetTextColor( 255, 255, 255, 255) surface.SetTextPos( 10, 80 ) surface.SetFont( "s1" ) if WEAP:IsValid() then surface.DrawText( "Weapon: "..myweapons[ WEAP:GetClass() ] ) else surface.DrawText( "Weapon: None") end end[/code]
[QUOTE=Robotboy655;42827273][code] -----------Weapons Table---------------- local myweapons = {} myweapons["weapon_crowbar"]="Crowbar" myweapons["weapon_fists"]="Fists" myweapons["weapon_pistol"]="Pistol" myweapons["weapon_357"]="357" myweapons["weapon_smg1"]="SMG" myweapons["weapon_ar2"]="Pulse Rifle" myweapons["weapon_shotgun"]="Shotgun" myweapons["weapon_crossbow"]="Crossbow" myweapons["weapon_frag"]="Grenade" myweapons["weapon_rpg"]="RPG" myweapons["weapon_slam"]="Slam" ----------------------------------------- surface.CreateFont("s1", { --s1 becomes the custom name of font size = 25, weight = 700, antialias = true, shadow = false, font = "ScoreboardText"}) local WEAP = LocalPlayer():GetActiveWeapon() surface.SetTextColor( 255, 255, 255, 255) surface.SetTextPos( 10, 80 ) surface.SetFont( "s1" ) if WEAP:IsValid() then surface.DrawText( "Weapon: "..myweapons[ WEAP:GetClass() ] ) else surface.DrawText( "Weapon: None") end end[/code][/QUOTE] I er uh... what? Thanks i guess. But id rather someone explain what was wrong so i would actually learn from it instead of just a c+p. But from your modified code i can learn a little. Thanks!
Sorry, you need to Log In to post a reply to this thread.