ProgrammingHelp with Lua coding
Posted:

ProgrammingHelp with Lua codingPosted:

Eli
  • Winter 2021
Status: Offline
Joined: Jun 12, 201311Year Member
Posts: 1,191
Reputation Power: 205
Status: Offline
Joined: Jun 12, 201311Year Member
Posts: 1,191
Reputation Power: 205
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:
-Deano
  • PC Master Race
Status: Offline
Joined: Aug 19, 201014Year Member
Posts: 5,243
Reputation Power: 532
Status: Offline
Joined: Aug 19, 201014Year Member
Posts: 5,243
Reputation Power: 532
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:
-Deano
  • Rated Awesome
Status: Offline
Joined: Aug 19, 201014Year Member
Posts: 5,243
Reputation Power: 532
Status: Offline
Joined: Aug 19, 201014Year Member
Posts: 5,243
Reputation Power: 532
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
         phys:Wake()
      end
   end

   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

   end
   
   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
      else
         net.Start( "PrinterMessage" ) -- Tell non-owners to bugger off!
            net.WriteString( "This is not your printer!" )
         net.Send( activator )
      end
      
   end
   
   function ENT:Think()
      if ( CurTime() > lastRegenTime + regenTime ) then -- If the time has increased by *regenTime*, give them the *regenAmount* money
         amount = amount + regenAmount
      end
   end
   
else
   
   net.Receive( "PrinterMessage", function( len, ply ) -- Print message to player chat
      local msg = net.ReadString()
      chat.AddText( msg )
   end )
   
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
maurits.tv/data/garrysmod/wiki/wi...x9268.html
Users browsing this topic: None
Jump to:


RECENT POSTS

HOT TOPICS