• What do you need help with V4
    639 replies, posted
  • [IMG]http://dl.dropbox.com/u/26392940/r1Bnl.png[/IMG] [B]March 2012 Edition[/B] Lua questions that don't deserve their own thread go here. This means things like: How to add jobs in DarkRP, Where to find PERP, A way to stop a player opening the console or main menu, getting rid of that ugly ass material and rounded box in the bottom left hand corner, a way to detect if the mouse cursor is enabled, How to shoot ponies out of a gun. and if your IQ is above 30, then things like why vehicles aren't working on Linux servers. [B]COOL STUFF TO LOOK AT! [/B][U][B]General Faqs[/B][/U]: [URL]http://www.facepunch.com/threads/868561-Need-help-READ-THIS-FIRST[/URL] [B][U]HOW TO LUA?[/U]:[/B] [url]http://wiki.garrysmod.com/?title=Lua_Tutorial_Series[/url] [U][B]GLUA WIKI[/B][/U]: [url]http://wiki.garrysmod.com[/url] [B]Previous threads that your probably wont look at:[/B] [URL=http://www.facepunch.com/threads/931152-What-do-you-need-help-with-V1]Thread 1[/URL] [URL=http://www.facepunch.com/threads/1068418]Thread 2[/URL] [URL=http://www.facepunch.com/threads/1160598]Thread 3[/URL]
  • May aswell start this off, I have this function from a script that Add's XP when you kill an NPC/Player. [lua] function AddEXP( ply, hitgroup, dmginfo ) local attacker = dmginfo:GetAttacker() if attacker:IsPlayer() then if EXP_STOPATMAX == 1 and attacker:GetNWInt("CurLevel") == levelups[table.Count(levelups)] then return end attacker:SetNWInt("Exp",attacker:GetNWInt("Exp")+EXP_INC) if table.HasValue(levelups,attacker:GetNWInt("Exp")) then local leveln = 1 for k,v in ipairs(levelups) do if v == attacker:GetNWInt("Exp") then leveln = k end end if leveln == table.Count(levelups) then PrintAll(attacker:Name().." has reached the maximum level!") else PrintAll(attacker:GetName().." has now reached level "..leveln.."!") end attacker:SetNWInt("NextLevel",levelups[leveln+1]) attacker:SetNWInt("CurLevel",levelups[leveln]) attacker:SetNWInt("LevelNum",leveln) attacker:SendLua("surface.PlaySound(\"achievements/achievement_earned.mp3\")") end end end [/lua] I'm needing a function so I can easily call ply:AddXP(amount of XP) or AddXP(ply, amount of XP) in my entire gamemode. I can see this [lua] attacker:SetNWInt("Exp",attacker:GetNWInt("Exp")+EXP_INC) [/lua] I know this sets there XP, but does the code below check if there XP has gotten enough for the next level? That would be pretty annoying to include that every time I increase there XP.
  • Avatar of Nerdeboy
  • [QUOTE=Mrkrabz;34952735]question about xp[/QUOTE] The problem with your code below is that it's checking if they have the exact amount of experience needed to level up. For example, if I have 900 xp, and need 1000 to level up, and killing this NPC gives me 100, then I will level up and everything works. But if killing the NPC gives me 180, I won't level up until I hit the next marker exactly. Also, unrelated, but I recommend making EXP_STOPATMAX into a boolean instead of an integer.
  • [QUOTE=Nerdeboy;34952970]The problem with your code below is that it's checking if they have the exact amount of experience needed to level up. For example, if I have 900 xp, and need 1000 to level up, and killing this NPC gives me 100, then I will level up and everything works. But if killing the NPC gives me 180, I won't level up until I hit the next marker exactly. Also, unrelated, but I recommend making EXP_STOPATMAX into a boolean instead of an integer.[/QUOTE] Oh I see what you mean, just now with this script 1 hit = 1xp so I would never have found that bug. How should I go about doing it? I've never really messed with something like this before.
  • Avatar of lorde banana
  • [QUOTE=Remscar;34949436]then things like why vehicles aren't working on Linux servers.[/QUOTE] if only someone would actually fix this it wouldn't need to be asked
  • [QUOTE=Banana Lord.;34953612]if only someone would actually fix this it wouldn't need to be asked[/QUOTE] Its sorta up to Garry. OT: Someone always rates the OP dumb...
  • Avatar of lorde banana
  • [QUOTE=Remscar;34954814]Its sorta up to Garry. OT: Someone always rates the OP dumb...[/QUOTE] and it's not like garry doesn't know about it, he personally locked two or three of my threads about it because they all turned into flame wars... and don't feel bad, CmdrMatthew rated Niggles V3 dumb too :(
  • Avatar of jakeabbott96
  • Hey there. I just started Lua literally the other day and I need some stupid help from someone with half more of a brain than myself. I'm making a really, really basic Derma panel so I can try and learn some Lua. I'm trying to make it so clicking the button will give me armour. That is literally all I need. [URL]http://maurits.tv/data/garrysmod/wiki/wiki.garrysmod.com/indexe102.html?title=Guide_to_Derma[/URL] That's the link I'm using to make the script in. [CODE]local DermaPanel = vgui.Create( "DFrame" ) -- Creates the frame itself DermaPanel:SetPos( 50,50 ) -- Position on the players screen DermaPanel:SetSize( 1000, 900 ) -- Size of the frame DermaPanel:SetTitle( "Testing Derma Stuff" ) -- Title of the frame DermaPanel:SetVisible( true ) DermaPanel:SetDraggable( true ) -- Draggable by mouse? DermaPanel:ShowCloseButton( true ) -- Show the close button? DermaPanel:MakePopup() -- Show the frame local DermaButton = vgui.Create( "DButton" ) DermaButton:SetParent( DermaPanel ) -- Set parent to our "DermaPanel" DermaButton:SetText( "Give ammo." ) DermaButton:SetPos( 25, 50 ) DermaButton:SetSize( 150, 50 ) DermaButton.DoClick = function () RunConsoleCommand( "givecurrentammo" ) -- What happens when you press the button end local DermaButton = vgui.Create( "DButton" ) DermaButton:SetParent( DermaPanel ) -- Set parent to our "DermaPanel" DermaButton:SetText( "Give armour." ) DermaButton:SetPos( 200, 50 ) DermaButton:SetSize( 150, 50 ) DermaButton.DoClick = function () RunConsoleCommand( ) end[/CODE] That's the script. Rate me boxes.
  • Avatar of Kidd
  • [QUOTE=jakeabbott96;34958907]Hey there. I just started Lua literally the other day and I need some stupid help from someone with half more of a brain than myself. I'm making a really, really basic Derma panel so I can try and learn some Lua. I'm trying to make it so clicking the button will give me armour. That is literally all I need. [URL]http://maurits.tv/data/garrysmod/wiki/wiki.garrysmod.com/indexe102.html?title=Guide_to_Derma[/URL] That's the link I'm using to make the script in. [CODE]local DermaPanel = vgui.Create( "DFrame" ) -- Creates the frame itself DermaPanel:SetPos( 50,50 ) -- Position on the players screen DermaPanel:SetSize( 1000, 900 ) -- Size of the frame DermaPanel:SetTitle( "Testing Derma Stuff" ) -- Title of the frame DermaPanel:SetVisible( true ) DermaPanel:SetDraggable( true ) -- Draggable by mouse? DermaPanel:ShowCloseButton( true ) -- Show the close button? DermaPanel:MakePopup() -- Show the frame local DermaButton = vgui.Create( "DButton" ) DermaButton:SetParent( DermaPanel ) -- Set parent to our "DermaPanel" DermaButton:SetText( "Give ammo." ) DermaButton:SetPos( 25, 50 ) DermaButton:SetSize( 150, 50 ) DermaButton.DoClick = function () RunConsoleCommand( "givecurrentammo" ) -- What happens when you press the button end local DermaButton = vgui.Create( "DButton" ) DermaButton:SetParent( DermaPanel ) -- Set parent to our "DermaPanel" DermaButton:SetText( "Give armour." ) DermaButton:SetPos( 200, 50 ) DermaButton:SetSize( 150, 50 ) DermaButton.DoClick = function () RunConsoleCommand( ) end[/CODE] That's the script. Rate me boxes.[/QUOTE] No reason to rate you boxes. You are trying to learn Lua/GLua. Though I will say you should of searched the wiki a little harder. [b][url=http://wiki.garrysmod.com/?title=Player.SetArmor]Player.SetArmor [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b] If you think there might be a function for it there probably is. If it has to do with the player then search the Player library on the wiki. Well you got to call it serverside though.
  • [QUOTE=Kidd;34959974]No reason to rate you boxes. You are trying to learn Lua/GLua. Though I will say you should of searched the wiki a little harder. [b][url=http://wiki.garrysmod.com/?title=Player.SetArmor]Player.SetArmor [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b] If you think there might be a function for it there probably is. If it has to do with the player then search the Player library on the wiki. [lua] local ply = LocalPlayer() -- Sets the player using this script to the variable ply local amount = 55 -- A variable called amount storing an integer to use with SetArmor function. local DermaPanel = vgui.Create( "DFrame" ) -- Creates the frame itself DermaPanel:SetPos( 50,50 ) -- Position on the players screen DermaPanel:SetSize( 1000, 900 ) -- Size of the frame DermaPanel:SetTitle( "Testing Derma Stuff" ) -- Title of the frame DermaPanel:SetVisible( true ) DermaPanel:SetDraggable( true ) -- Draggable by mouse? DermaPanel:ShowCloseButton( true ) -- Show the close button? DermaPanel:MakePopup() -- Show the frame local DermaButton = vgui.Create( "DButton" ) DermaButton:SetParent( DermaPanel ) -- Set parent to our "DermaPanel" DermaButton:SetText( "Give ammo." ) DermaButton:SetPos( 25, 50 ) DermaButton:SetSize( 150, 50 ) DermaButton.DoClick = function () RunConsoleCommand( "givecurrentammo" ) -- What happens when you press the button end local DermaButton = vgui.Create( "DButton" ) DermaButton:SetParent( DermaPanel ) -- Set parent to our "DermaPanel" DermaButton:SetText( "Give armour." ) DermaButton:SetPos( 200, 50 ) DermaButton:SetSize( 150, 50 ) DermaButton.DoClick = function () ply:SetArmor(amount) -- Player.SetArmor() You change Player to the variable holding the LocalPlayer and insert the variable (or just a number) of the amount you want. end[/lua][/QUOTE] That's what I originally posted above, but I thought this derma script was Client sided? SetArmour is Server sided. I'm probably wrong but just a thought.
  • Avatar of Kidd
  • [QUOTE=Mrkrabz;34960119]That's what I originally posted above, but I thought this derma script was Client sided? SetArmour is Server sided. I'm probably wrong but just a thought.[/QUOTE] It's shared I believe.
  • [QUOTE=Kidd;34960153]It's shared I believe.[/QUOTE] [url]http://maurits.tv/data/garrysmod/wiki/wiki.garrysmod.com/index354e.html?title=Player.SetArmor[/url] is server sided But checking the Client's armour is shared.
  • Avatar of Kidd
  • [QUOTE=Mrkrabz;34960222][url]http://maurits.tv/data/garrysmod/wiki/wiki.garrysmod.com/index354e.html?title=Player.SetArmor[/url] is server sided But checking the Client's armour is shared.[/QUOTE] Well then ignore what I said. He will need to call it serverside then.
  • [QUOTE=Nerdeboy;34952970]The problem with your code below is that it's checking if they have the exact amount of experience needed to level up. For example, if I have 900 xp, and need 1000 to level up, and killing this NPC gives me 100, then I will level up and everything works. But if killing the NPC gives me 180, I won't level up until I hit the next marker exactly. Also, unrelated, but I recommend making EXP_STOPATMAX into a boolean instead of an integer.[/QUOTE] I see what you mean, I tried doing this: [lua] function AddEXP( ply, args ) ply:SetNWInt("Exp",ply:GetNWInt("Exp")+args) if table.HasValue(levelups,ply:GetNWInt("Exp")) then local leveln = 1 for k,v in ipairs(levelups) do if v == ply:GetNWInt("Exp") then leveln = k end end if leveln == table.Count(levelups) then PrintAll(ply:Name().." has reached the maximum level!") else PrintAll(ply:GetName().." has now reached level "..leveln.."!") end ply:SetNWInt("NextLevel",levelups[leveln+1]) ply:SetNWInt("CurLevel",levelups[leveln]) ply:SetNWInt("LevelNum",leveln) ply:SendLua("surface.PlaySound(\"achievements/achievement_earned.mp3\")") end end [/lua] And got the bug that you mentioned. I'm not entirely sure on how to fix this, Line 6 is what checks right? I also tested it by calling it with [lua] concommand.Add("exp_setlevel",function(ply,cmd,args) if ply:IsAdmin() then AddEXP( ply, tonumber(args[1]) ) end end) table.insert(chatcommands,{"setlevel","exp_setlevel"}) [/lua] It works but my XP goes over the needed amount as you said it would.
  • [QUOTE=Mrkrabz;34960776]I see what you mean, I tried doing this: [lua] function AddEXP( ply, args ) ply:SetNWInt("Exp",ply:GetNWInt("Exp")+args) if table.HasValue(levelups,ply:GetNWInt("Exp")) then local leveln = 1 for k,v in ipairs(levelups) do if v == ply:GetNWInt("Exp") then leveln = k end end if leveln == table.Count(levelups) then PrintAll(ply:Name().." has reached the maximum level!") else PrintAll(ply:GetName().." has now reached level "..leveln.."!") end ply:SetNWInt("NextLevel",levelups[leveln+1]) ply:SetNWInt("CurLevel",levelups[leveln]) ply:SetNWInt("LevelNum",leveln) ply:SendLua("surface.PlaySound(\"achievements/achievement_earned.mp3\")") end end [/lua] And got the bug that you mentioned. I'm not entirely sure on how to fix this, Line 6 is what checks right? I also tested it by calling it with [lua] concommand.Add("exp_setlevel",function(ply,cmd,args) if ply:IsAdmin() then AddEXP( ply, tonumber(args[1]) ) end end) table.insert(chatcommands,{"setlevel","exp_setlevel"}) [/lua] It works but my XP goes over the needed amount as you said it would.[/QUOTE] Instead check to see if the XP is greater than or equal to, not if it is exactly equal to. Also, do you need to store all of that information in NWInts. All you really need is one NWInt, since NextLevel and CurLevel are both based on LevelNum.. If the levelups table exists clientside, you can just get the rest of that information clientside as well.
  • Avatar of jakeabbott96
  • I tried at the armour again, but I got a new error: [lua\derma.lua:31] attempt to call method 'SetArmor' (a nil value) [CODE]local ply = LocalPlayer(Player) -- Sets the player using this script to the variable ply local amount = 55 -- A variable called amount storing an integer to use with SetArmor function. local DermaPanel = vgui.Create( "DFrame" ) -- Creates the frame itself DermaPanel:SetPos( 50,50 ) -- Position on the players screen DermaPanel:SetSize( 1000, 900 ) -- Size of the frame DermaPanel:SetTitle( "Testing Derma Stuff" ) -- Title of the frame DermaPanel:SetVisible( true ) DermaPanel:SetDraggable( true ) -- Draggable by mouse? DermaPanel:ShowCloseButton( true ) -- Show the close button? DermaPanel:MakePopup() -- Show the frame local DermaButton = vgui.Create( "DButton" ) DermaButton:SetParent( DermaPanel ) -- Set parent to our "DermaPanel" DermaButton:SetText( "Give ammo." ) DermaButton:SetPos( 25, 50 ) DermaButton:SetSize( 150, 50 ) DermaButton.DoClick = function () RunConsoleCommand( "givecurrentammo" ) -- What happens when you press the button end local DermaButton = vgui.Create( "DButton" ) DermaButton:SetParent( DermaPanel ) -- Set parent to our "DermaPanel" DermaButton:SetText( "Give armour." ) DermaButton:SetPos( 200, 50 ) DermaButton:SetSize( 150, 50 ) DermaButton.DoClick = function () ply:SetArmor(100) -- Player.SetArmor() You change Player to the variable holding the LocalPlayer and insert the variable (or just a number) of the amount you want. end [/CODE] I'm trying to make clicking the button give me, the player and client, some armour. I'm very new to Lua, by the by.
  • Avatar of ArmageddonScr
  • you're trying to use SetArmor on the client. You have to use it on the server. In a shared file, this is what your code would look like. [lua] if SERVER then concommand.Add("setarmor",function(pl) pl:SetArmor(100) end) else --menu code here, and DermaButton.DoClick=function() RunConsoleCommand("setarmor") end end [/lua]
  • [QUOTE=Persious;34958828]Fast question, is there a list of sounds in GMod somewhere?[/QUOTE] [b][url=http://maurits.tv/data/garrysmod/wiki/wiki.garrysmod.com/index8f77.html?title=Half-Life_2_Sound_List]Half-Life 2 Sound List [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b]
  • Avatar of jakeabbott96
  • [QUOTE=ArmageddonScr;34963443]you're trying to use SetArmor on the client. You have to use it on the server. In a shared file, this is what your code would look like. [lua] if SERVER then concommand.Add("setarmor",function(pl) pl:SetArmor(100) end) else --menu code here, and DermaButton.DoClick=function() RunConsoleCommand("setarmor") end end [/lua][/QUOTE] The console says there is no "setarmor" command. [lua] local ply = LocalPlayer(Player) -- Sets the player using this script to the variable ply local amount = 55 -- A variable called amount storing an integer to use with SetArmor function. local DermaPanel = vgui.Create( "DFrame" ) -- Creates the frame itself DermaPanel:SetPos( 50,50 ) -- Position on the players screen DermaPanel:SetSize( 1000, 900 ) -- Size of the frame DermaPanel:SetTitle( "Derma panel." ) -- Title of the frame DermaPanel:SetVisible( true ) DermaPanel:SetDraggable( true ) -- Draggable by mouse? DermaPanel:ShowCloseButton( true ) -- Show the close button? DermaPanel:MakePopup() -- Show the frame local DermaButton = vgui.Create( "DButton" ) DermaButton:SetParent( DermaPanel ) -- Set parent to our "DermaPanel" DermaButton:SetText( "Give ammo." ) DermaButton:SetPos( 25, 50 ) DermaButton:SetSize( 150, 50 ) DermaButton.DoClick = function () RunConsoleCommand( "givecurrentammo" ) -- What happens when you press the button end if SERVER then concommand.Add("setarmor",function(pl) pl:SetArmor(100) end) else local DermaButton = vgui.Create( "DButton" ) DermaButton:SetParent( DermaPanel ) -- Set parent to our "DermaPanel" DermaButton:SetText( "Give armour." ) DermaButton:SetPos( 200, 50 ) DermaButton:SetSize( 150, 50 ) --menu code here, and DermaButton.DoClick=function() RunConsoleCommand("setarmor") end end [/lua]
  • Avatar of jakeabbott96
  • [QUOTE=Banana Lord.;34965919]you are only running this on the client...[/QUOTE] I'm totally new to Lua. This is just my first attempt at actually trying to produce anything.
  • Avatar of lorde banana
  • [QUOTE=jakeabbott96;34965953]I'm totally new to Lua. This is just my first attempt at actually trying to produce anything.[/QUOTE] well, I'll try to explain, the concepts are pretty simple lua/autorun - files in this are run on the client and server lua/autorun/client - files in this are run ONLY on the client lua/autorun/server - files in this are run ONLY on the server you should probably put your menu stuff in autorun/client and serverside stuff in autorun/server
  • Avatar of jakeabbott96
  • [QUOTE=Banana Lord.;34966033]well, I'll try to explain, the concepts are pretty simple lua/autorun - files in this are run on the client and server lua/autorun/client - files in this are run ONLY on the client lua/autorun/server - files in this are run ONLY on the server you should probably put your menu stuff in autorun/client and serverside stuff in autorun/server[/QUOTE] Okay, that I can understand. But why isn't the Lua script working?
  • Avatar of lorde banana
  • [QUOTE=jakeabbott96;34966058]Okay, that I can understand. But why isn't the Lua script working?[/QUOTE] because you only ran that script on the client, so the server bit didn't run