I need help learning lua

I’d like to make entities using lua and possibly a gamemode. I’ve already read the lua guide on the gmod wiki page and all that stuff didn’t really help me a lot, i only know a little (i emphasize little) about programing in C++ but i want to know commands that would help me make an entity. it would also be great if you could possibly teach me.

The best way to learn how to code entities is to go and look at other entities. Look at what something does(The Bouncy Ball) and then open the .lua file for it and read the code and try to make sense of it all little by little.

yeah, I’ve dissected a few sents, I want to know other kinds of commands and stuff and any tips from developers

I looked into the bouncy ball sent and i added a new color by adding this in the cl_init:

[lua]function ENT:Initialize()

local i = math.random( 0, 4 )

if ( i == 0 ) then
	self.Color = Color( 255, 0, 0, 255 )
elseif ( i == 1 ) then
	self.Color = Color( 0, 255, 0, 255 )
elseif ( i == 2 ) then
	self.Color = Color( 255, 255, 0, 255 )
elseif ( i == 3 ) then
	self.Color = Color( 255, 0, 255, 255 ) // I added this new color but I dont know what color it is, lol
	self.Color = Color( 0, 0, 255, 255 )

self.LightColor = Vector( 0, 0, 0 )

end [/lua]

You add
[ lua] and [ /lua]

before and after your code without the space of course.

Good job on the new color as well!


Explanation of your color…

There are 4 values for colors. The first is Red, Second is Green, Third is Blue, Fourth is Alpha.
Obviously the first RGB makes up all the colors that it will be. The last one is Alpha. Also known as transparency. So if you want it to be see through you can lower that value. All values are 0-255.

That color that you added is pink.

My bad, I posted the wrong code to you. I forgot this is facepunch. Haha I edited my previous post in reguards to how to make it show up as code on the forums.

I’ve made my own SENT and I’m trying to make the SENT, which uses the combine helicopter bomb model, to use a custom texture I made for it so far I’ve gotten this:


local matCombineBall = Material( “materials/models/Combine_Helicopter” )

function ENT:Draw()


render.SetMaterial( matCombineBall ) 

this is in my cl_init, I took some lines from the bouncy ball and i cant get it to use my texture, the SENT is in proper addon format, using an info.txt and proper file paths, but I have no luck in getting it to work. any tips?

You need to set the material before you draw the entity. Or you could use **[Entity.SetMaterial


If you don’t have a spawn function and you haven’t set ENT.Spawnable in shared.lua to true, it won’t show up in your ents tab.

thanks, i put the render.SetMaterial right after the local matCombineBall = Material( “materials/models/Combine_Helicopter” ) hopefully it works, trying it now.


didn’t work, maybe putting self.Entity:SetMaterial( “materials/models/Combine_Helicopter” ) in the ENT:Initialize function should do the trick?

I can’t find out where to put the entity.SetMaterial. care to enlighten me?


Good Place. http://wiki.garrysmod.com/?title=Lua

yeah, I’ve read some of that and I don’t really understand it.


this is getting really annoying, I’ve tried changing the scripts so many times and all I get is the helicopter bomb having no texture at all (making it invisible.) can anyone help me make it have a material? and tell me where exactly to put it

You need to put self:SetMaterial(“material”) in your ENT:Initialize function.

it still doesn’t work, the helicopter bomb is invisible but you can still see the shadow under it. I’ve tried spawning it on 2 different maps with the same results. Is there anything I did wrong? I put the material for it in the materials/models/Combine_Helicopter/ folder, It’s the same path that the regular helicopter bomb uses. should I make a new file path or what?


right now I have it set to: self:SetMaterial(“materials/models/bomb”), I tried “materials/models/bomb/helicopter_bomb_off01” and tried putting .vtf at the end. I have no idea what I am doing wrong and no clue what to do, by the way I put this in my init.lua and it keeps spawning invisible.


here’s my the start of my init:

[lua]AddCSLuaFile( “cl_init.lua” )
AddCSLuaFile( “shared.lua” )
include( ‘shared.lua’ )

function ENT:SpawnFunction( ply, tr )

if ( !tr.Hit ) then return end

local SpawnPos = tr.HitPos + tr.HitNormal * 16

local ent = ents.Create( "10hpball" )
ent:SetPos( SpawnPos )

return ent


Name: Initialize
function ENT:Initialize()

// Use the helibomb model just for the shadow (because it's about the same size)
self.Entity:SetModel( "models/Combine_Helicopter/helicopter_bomb01.mdl" )
self.Entity:PhysicsInit( SOLID_VPHYSICS )
self.Entity:SetMoveType( MOVETYPE_VPHYSICS )
self.Entity:SetSolid( SOLID_VPHYSICS )
self.Entity:SetUseType( SIMPLE_USE )

// Wake the physics object up. It's time to have fun!
local phys = self.Entity:GetPhysicsObject()
if (phys:IsValid()) then


my shared is all correct and it shows up in my entities tab, here’s my cl_init:


function ENT:Draw()



ok, I changed the function ENT:Draw() and now it’s not invisible anymore, it still uses the default texture, is there anything I can do for that?

The SetMaterial function takes a path relative to the materials folder, you don’t need to do “materials/models/etc”, instead you should do “models/etc”.

YES!!! SUCCESS!!! It finally worked, thanks MakeR for helping me find out how to set the material, oh and do you know how to make it make a sound when I press use? I’m guessing it would go in the ENT:Use function. Is there anything special I need to do to make it work?

You need to set the use mode to SIMPLE_USE to prevent the use function being called many times at once. **[Entity.SetUseType

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=Entity.SetUseType)** - you should set it in ENT.Initialize. Then you just need to use **[Entity.EmitSound

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=Entity.EmitSound)** in ENT.Use.

thanks, I also want to know how to make it say something in a little balloon, like with the life support generators.

I think **[G.AddWorldTip

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=G.AddWorldTip)** is what you are looking for then.

how do I use this one?