• Value changing console command
    14 replies, posted
I am trying to change a variable's value using a command, as in "Answer 10" and it should change the variable to "10", but of course as you can tell I don't know how and can't really find any answers either.. so.. anyone? ~thanks
[code] local var = 69420 concommand.Add("test_cmd_1", function(ply, cmd, args) var = tonumber(args[1]) end) [/code]
[QUOTE=tgandrew2468;52161064][code] local var = 69420 concommand.Add("test_cmd_1", function(ply, cmd, args) var = tonumber(args[1]) end) [/code][/QUOTE] thanks =)
Okay so I thought that I figured this out, but apparently I did not... [CODE]local ArmReq = 0 concommand.Add( "SetArmor" , function( ply, cmd , args ) ArmReq = args[1] ply:SetArmor( ArmReq ) end ) print( ArmReq ) [/CODE] I write "SetArmor 10" in console and the error I am getting is that "ArmReq" is nil as in basically not being set to 10.. Could anyone explain this? The print is just so I could see if its setting the var.. note: new to lua and just making a hud with bars and all that
If you aren't going to use ArmReq outside of the concommand callback, then there is no need to define it outside of the function. Next, I would add a failsafe to make sure args [1] exists before you send it into SetArmor so that just running the command with no arguments doesn't error. Lastly, the args table is made up of strings, and Player.SetArmor accepts numbers, so you'll have to use the tonumber function as shown by tgandrew above.
[QUOTE=code_gs;52162091]If you aren't going to use ArmReq outside of the concommand callback, then there is no need to define it outside of the function. Next, I would add a failsafe to make sure args [1] exists before you send it into SetArmor so that just running the command with no arguments doesn't error. Lastly, the args table is made up of strings, and Player.SetArmor accepts numbers, so you'll have to use the tonumber function as shown by tgandrew above.[/QUOTE] by failsafe, do you mean something along the lines of [code] if args[1] == ArmReq then ply:SetArmor( ArmReq ) [/code] ? Again, new to this. Also, I think I got it now and now i'm having a problem with what seems to be defining the actual player I would like to set the armor of, which is myself. or maybe I'm just reading it wrong.. [code] [ERROR] lua/armorsetconcom.lua:6: Tried to use a NULL entity! 1. SetArmor - [C]:-1 2. unknown - lua/armorsetconcom.lua:6 3. unknown - lua/includes/modules/concommand.lua:54 [/code] Edit: I think I just realized that I might need to actually contact client in order to get the "localplayer" name as it prints null if command is written through the dedicated console..
[QUOTE=Crooks_;52162158]by failsafe, do you mean something along the lines of [code] if args[1] == ArmReq then ply:SetArmor( ArmReq ) [/code] ? [/QUOTE] No, he means [lua]if args[1] then -- Do something with args[1] end[/lua] Also are you running it from the client or the server?
[QUOTE=JasonMan34;52162179]No, he means [lua]if args[1] then -- Do something with args[1] end[/lua] Also are you running it from the client or the server?[/QUOTE] server
The issue with your provided example is that you're not converting args[1] into a number. Console args will always be returned as strings. You're essentially doing ply:SetArmor("10"), which wouldn't work since SetArmor() only takes ints. Just tested this code and it works just fine @lua/autorun/server [code] concommand.Add("set_armor", function(ply, cmd, args) if args[1] then ply:SetArmor(tonumber(args[1])) end end) [/code]
[QUOTE=tgandrew2468;52162609]The issue with your provided example is that you're not converting args[1] into a number. Console args will always be returned as strings. You're essentially doing ply:SetArmor("10"), which wouldn't work since SetArmor() only takes ints. Just tested this code and it works just fine @lua/autorun/server [code] concommand.Add("set_armor", function(ply, cmd, args) if args[1] then ply:SetArmor(tonumber(args[1])) end end) [/code][/QUOTE] While what he's saying is correct, you're also trying to give the console armor. [B]ply[/B] in that function is the player that called it. Since you ran it from the console, there is no player, so [B]ply[/B] is undefined. Who is supposed to be getting armor?
[QUOTE=JasonMan34;52162641]While what he's saying is correct, you're also trying to give the console armor. [B]ply[/B] in that function is the player that called it. Since you ran it from the console, there is no player, so [B]ply[/B] is undefined. Who is supposed to be getting armor?[/QUOTE] I'm not running it from the server console. [code] ] set_armor 674 ] lua_run_cl print(LocalPlayer():Armor()) 674 [/code]
[QUOTE=tgandrew2468;52162651]I'm not running it from the server console. [code] ] set_armor 674 ] lua_run_cl print(LocalPlayer():Armor()) 674 [/code][/QUOTE] You're not the OP
[QUOTE=tgandrew2468;52162609]The issue with your provided example is that you're not converting args[1] into a number. Console args will always be returned as strings. You're essentially doing ply:SetArmor("10"), which wouldn't work since SetArmor() only takes ints. Just tested this code and it works just fine @lua/autorun/server [code] concommand.Add("set_armor", function(ply, cmd, args) if args[1] then ply:SetArmor(tonumber(args[1])) end end) [/code][/QUOTE] I think I understand now, thanks.
[QUOTE=JasonMan34;52163951]You're not the OP[/QUOTE] You replied to [b]my[/b] post and were refering to someone as "your". What else was I to think?
[QUOTE=tgandrew2468;52165092]You replied to [b]my[/b] post and were refering to someone as "your". What else was I to think?[/QUOTE] I referred to you as "he". And since the OP said he's using running it from the server, I thought it was clear when I said "you're running it from the server"
Sorry, you need to Log In to post a reply to this thread.