• Variable help
    4 replies, posted
[code] gold = 1000 function GM:HUDPaint() self.BaseClass:HUDPaint() local ply = LocalPlayer() local gold = LocalPlayer():gold() surface.CreateFont( "ScoreboardText", 40, 250, true, false, "goldfont" ) surface.SetTextColor( 255, 255, 255, 255) surface.SetTextPos( 36, 15 ) surface.SetFont( "goldfont" ) surface.DrawText( "$", gold ) end [/code] in-game, it constantly complains that the variable gold does not exist. but it DOES as you can see 'gold = 1000' that's how a variable is defined right? How could I display a custom variable on a HUD (meaning NOT health or ammo) without errors? Thanks. EDIT: one more question, if I put the variable in shared.lua, when it is modified serverside it will update the client right? How could I make it be a 'shared' variable, or will it be one? EDIT2: Full Error [code] [ERROR] gamemodes/casino/gamemode/cl_init.lua:15: attempt to call method 'gold' (a nil value) [/code]
You redefined gold as local gold = LocalPlayer():gold(). (line 6 in your code) Remove that line. LocalPlayer():gold() isn't anything. EDIT: Shared variables won't update for the client when you update them on the server. You need to send the client the variable with the [URL=http://gmodwiki.net/Lua/Libraries/net]net[/URL], [URL=http://gmodwiki.net/Lua/Libraries/umsg]umsg[/URL]/[URL=http://gmodwiki.net/Lua/Libraries/usermessage]usermessage[/URL], and (in the case of client-server) [URL=http://gmodwiki.net/Lua/Libraries/concommand]concommand[/URL] libraries. You can also use [url=http://glua.me/search/?keywords=networked][img]http://glua.me/search.png[/img] [b]networked variables[/b][/url] and [url=http://glua.me/search/?keywords=tdt][img]http://glua.me/search.png[/img] [b]DT Vars[/b][/url].
You're trying to define gold as a player function, make a MetaTable for that.
[QUOTE=bobbleheadbob;40714021]You redefined gold as local gold = LocalPlayer():gold(). (line 6 in your code) Remove that line. LocalPlayer():gold() isn't anything. EDIT: Shared variables won't update for the client when you update them on the server. You need to send the client the variable with the [URL=http://gmodwiki.net/Lua/Libraries/net]net[/URL], [URL=http://gmodwiki.net/Lua/Libraries/umsg]umsg[/URL]/[URL=http://gmodwiki.net/Lua/Libraries/usermessage]usermessage[/URL], and (in the case of client-server) [URL=http://gmodwiki.net/Lua/Libraries/concommand]concommand[/URL] libraries. You can also use [url=http://glua.me/search/?keywords=networked][img]http://glua.me/search.png[/img] [b]networked variables[/b][/url] and [url=http://glua.me/search/?keywords=tdt][img]http://glua.me/search.png[/img] [b]DT Vars[/b][/url].[/QUOTE] Thanks, but is there a solution to display custom variables on the HUD? I know it fixes when I remove that line but what is the right way to do it?
[QUOTE=RonanZer0;40714206]Thanks, but is there a solution to display custom variables on the HUD? I know it fixes when I remove that line but what is the right way to do it?[/QUOTE] The way you are trying to do it is the correct way. You just had that line which was botching everything up. To make a HUD display custom variables, just use a variable for what the HUD displays and change that variable whenever you want. Changing the variable will change the HUD in real time. For example, the following script will add 1 pwn per second to your HUD. Try running it to see what I mean. [lua] local pwn = 0 hook.Add("HUDPaint", "Pwn", function() draw.SimpleText(pwn, "HUDNumber1", 50, 50, Color(100,100,100)) end) function AddPwn() pwn = pwn+1 timer.Simple(1, AddPwn) end timer.Simple(1, AddPwn) [/lua] Also, your surface.DrawText() is done weird. If you want to stick two pieces of text together, don't add a comma, add two dots. (..) [lua] print("haha".." You're ".." Fu".."nny"..".") --This would print --haha You're Funny. --into the console. [/lua] And lastly, surface.SetColor(255,255,255,255) should be surface.SetColor(Color(255,255,255,255))
Sorry, you need to Log In to post a reply to this thread.