What do you need help with V4


March 2012 Edition

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.

**General Faqs: http://www.facepunch.com/threads/868561-Need-help-READ-THIS-FIRST
HOW TO LUA?: http://wiki.garrysmod.com/?title=Lua_Tutorial_Series
GLUA WIKI: http://wiki.garrysmod.com

Previous threads that your probably wont look at:
Thread 1
Thread 2
Thread 3

May aswell start this off, I have this function from a script that Add’s XP when you kill an NPC/Player.

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

	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


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

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.

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.

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.

Just check if the value is equal to or greater than.

if only someone would actually fix this it wouldn’t need to be asked

Its sorta up to Garry.

OT: Someone always rates the OP dumb…

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 :frowning:

Fast question, is there a list of sounds in GMod somewhere?

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.

That’s the link I’m using to make the script in.

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

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(  )

That’s the script.

Rate me boxes.


Is there anyway to get a props weight or even size?

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.



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.

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.

It’s shared I believe.

is server sided

But checking the Client’s armour is shared.

Well then ignore what I said.

He will need to call it serverside then.


I see what you mean, I tried doing this:

function AddEXP( ply, 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


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
if ply:IsAdmin() then
AddEXP( ply, tonumber(args[1]) )

It works but my XP goes over the needed amount as you said it would.