ProgrammingHelp with Lua codingPosted:

  • Summer 2019
Status: Offline
Joined: Jun 12, 20136 Year Member
Posts: 930
Reputation Power: 161
I've been trying to code printers (garry's mod) for about the past hour but spawning them isn't working. I've watched tutorials read tons of posts online yet nothing has helped. Any help would be appreciated.

You can contact me on discord (Java#5539) or here on TTG
#2. Posted:
  • PC Master Race
Status: Offline
Joined: Aug 19, 20109 Year Member
Posts: 5,206
Reputation Power: 522
If you post what code you have, or exactly what you are trying to achieve I can give you some help.
I've done a lot of lua with with gmod.
#3. Posted:
  • Rated Awesome
Status: Offline
Joined: Aug 19, 20109 Year Member
Posts: 5,206
Reputation Power: 522
Just as a quick shitty example of a printer (not tested due to writing this straight into the TTG reply box but you should get the general idea)

ENT.Type = "anim"
ENT.Base = "base_gmodentity"

ENT.PrintName = "MyPrinter"
ENT.Author = "Deano"
ENT.Contact = "MyContactInfo"
ENT.Purpose = "Purpose"
ENT.Instructions = "Spawn in Q menu. Press E to take money out"
ENT.Spawnable = true
ENT.AdminSpawnable = true

local owner

local amount = 0
local regenAmount = 10
local regenTime = 10
local lastRegenTime = CurTime()

if ( SERVER ) then
   util.AddNetworkString( "PrinterMessage" )
   function ENT:Initialize()
      self:SetModel( "MyModel.mdl" )
      self:PhysicsInit( SOLID_VPHYSICS )
      self:SetMoveType( MOVETYPE_VPHYSICS )
      self:SetSolid( SOLID_VPHYSICS )
      local phys = self:GetPhysicsObject()
      if ( phys:IsValid() ) then

   function ENT:SpawnFunction( ply, tr, ClassName )

      if ( !tr.Hit ) then return end -- If the player isn't aiming anywhere that we can place the entity, don't spawn it

      owner = ply -- Set whoever spawned it in to be the owner

      local spawnPos = tr.HitPos + tr.HitNormal * 10 -- Spawn the entity a short distance away from the player
      local spawnAng = ply:EyeAngles() -- Where is the player aiming towards
      spawnAng.p = 0 -- Reset the pitch of the entity (so it is placed flat)
      spawnAng.y = spawnAng.y + 180 -- Make the entity face the player

      local ent = ents.Create( ClassName ) -- Create an instance of the entity
      ent:SetPos( spawnPos ) -- Set the spawn position
      ent:SetAngles( spawnAng ) -- Set the angle
      ent:Spawn() -- Spawn it into the world
      ent:Activate() -- Activate it to apply physics

      return ent

   function ENT:Use( activator, caller )
      if ( activator:SteamID64() == owner:SteamID64() ) then -- the person interacting is the owner
         local balance = owner:getDarkRPVar( "balance" ) -- Get their current DarkRP balance
         owner:setDarkRPVar( "balance", balance + amount ) -- Increase their balance
         net.Start( "PrinterMessage" ) -- Send a chat message that they've received the money
            net.WriteString( "You have gained " .. amount .. "!" )
         net.Send( owner )
         amount = 0 -- reset the current amount in printer
         net.Start( "PrinterMessage" ) -- Tell non-owners to bugger off!
            net.WriteString( "This is not your printer!" )
         net.Send( activator )
   function ENT:Think()
      if ( CurTime() > lastRegenTime + regenTime ) then -- If the time has increased by *regenTime*, give them the *regenAmount* money
         amount = amount + regenAmount
   net.Receive( "PrinterMessage", function( len, ply ) -- Print message to player chat
      local msg = net.ReadString()
      chat.AddText( msg )
   end )

You'll have to forgive the syntax highlighting as TTG defaults to C-based but I don't know how to change it to be lua.

Provided that you have DarkRP installed, you should be able to find a printer entity in there by default and should just copy and paste the files (whilst changing the name too) and then change the values within the new printer files;

The guide linked below is an old example but should be pretty useful if you want to make a fully customised printer
Users browsing this topic: None
Jump to: