Random money printer color every 3 seconds

Hello. I would like to make my money printer change color every 3 seconds. I have figured out how to make the printer spawn as a random color every time a player buys it, but I want to add a timer so that the color of the printer model will change every 3 seconds.

Code for making the printer spawn as a random color:


ent.printerColor = Color(math.random(0,255),math.random(0,255),math.random(0,255), 255 );

You could use a loop.

When the entity is create you would one to run a timer to set the color at what ever time frame you want.

No, JUST NO!



timer.Create( "IEatDankMemes", 3, 0, function()
    local r, g, b = math.random(0,255), math.random(0,255), math.random(0,255)
    ent.printerColor = Color(r, g, b)
end)


I donno, something like this?

A better idea would be along the lines of:
[lua]if CurTime() >= ent.nextcolorchange then
ent.nextcolorchange = CurTime()+3
ent.printerColor = – your random color here
end[/lua]

Oh ok. Thanks from myself, I’ll keep something like that in mind for a next time :slight_smile:

This is what I have


local function SetValues( ent )
	ent.printTime = 10; -- Default print time.
	ent.minPrint = 5; -- Minimum print amount.
	ent.maxPrint = 2000; -- Maximum print amount.
	ent.upgradedExtra = ent.maxPrint * 0.5; -- The additional income received on upgraded printers.
	if CurTime() >= ent.nextcolorchange then
    ent.nextcolorchange = CurTime()+3
    ent.printerColor = Color(math.random(0,255),math.random(0,255),math.random(0,255), 255 );
	end
	--ent.printerColor = Color(math.random(0,255),math.random(0,255),math.random(0,255), 255 );
	ent.coolantSystem = false; -- Toggles the coolant system.
	ent.coolantLoss = 0.5; -- The Percentage loss for each print of the coolant is enabled.
end;

This is the error it gives me



[ERROR] addons/darkrpmodification-master/lua/entities/k_printer_t8/init.lua:11: attempt to compare nil with number
  1. SetValues - addons/darkrpmodification-master/lua/entities/k_printer_t8/init.lua:11
   2. unknown - addons/darkrpmodification-master/lua/entities/k_printer_t8/init.lua:30
    3. Spawn - [C]:-1
     4. defaultSpawn - gamemodes/darkrp/gamemode/modules/base/sh_createitems.lua:363
      5. callback - gamemodes/darkrp/gamemode/modules/base/sh_createitems.lua:414
       6. callback - gamemodes/darkrp/gamemode/modules/chat/sv_chat.lua:17
        7. unknown - gamemodes/darkrp/gamemode/modules/chat/sv_chat.lua:255
         8. unknown - lua/includes/modules/concommand.lua:54



You didn’t define ent.nextcolorchange in the first place

I thought this was defining ent.nextcolorchange ? Sorry man, I’m a lua noob haha.


ent.nextcolorchange = CurTime()+3

That line only happens after the comparison takes place, so the first time you’re trying to compare, the error happens.

I intentionally left the initialization as an exercise for you, which I think that you can solve with a little bit of thought and dedication. Hint: the first time this code runs, ent.nextcolorchange is nil. In that case you should probably set it to 0 before doing anything.

Credits to Vrondakis for this piece of code.


	
function ENT:Think()
	if(self.DarkRPItem.vrondakisEpileptic) then
		if(self.StoredMoney>0) then -- IF THERE IS MONEY IN THE PRINTER
			-- Pick a random color, go to it, then change the color
			local Rr = math.random(0,255)
			local Rb = math.random(0,255)
			local Rc = math.random(0,255)

			self:SetColor(Color(Rr,Rb,Rc))
		end
	end

end


I changed my code to this.


local function SetValues( ent )
	ent.printTime = 10; -- Default print time.
	ent.minPrint = 5; -- Minimum print amount.
	ent.maxPrint = 2000; -- Maximum print amount.
	ent.upgradedExtra = ent.maxPrint * 0.5; -- The additional income received on upgraded printers.
	ent.nextcolorchange = 4
	if CurTime() >= ent.nextcolorchange then
    ent.nextcolorchange = CurTime()+3
    ent.printerColor = Color(math.random(0,255),math.random(0,255),math.random(0,255), 255 );
	end
	--ent.printerColor = Color(math.random(0,255),math.random(0,255),math.random(0,255), 255 );
	ent.coolantSystem = false;
	ent.coolantLoss = 0.5;
end;

This makes the printer spawn with a random color, but the color wont change. I tried this aswell,


ent.nextcolorchange = math.random(0,255)

The above code just makes the printer spawn with the color as a certain shade of purple, and only that shade of purple.

I’m sorry for being such a noob and bothering you man, thank you so much for your help im just confused haha.

I am assuming SetValues is only called once, so it only checks if CurTime() >= nextcolorchange once. You need to have it in something that is always being ran, like in ENT:Think()

Here is my ENT:Think


function ENT:Think()
	ent.nextcolorchange = CurTime()+3
	if CurTime() >= ent.nextcolorchange then
		ent.printerColor = Color(math.random(0,255),math.random(0,255),math.random(0,255), 255 );
	end
	if self.damage then
		if ( self.damage <= 0 ) then
			if not self:IsOnFire() then
				self.damage = 0;
				self:SetNWInt( "Health", 0 );
				
				self:BurstIntoFlames();	
			end;
		end;
	end;

	if self:WaterLevel() > 0 then
		self:Destruct();
		self:Remove();
		return;
	end;

	if not self.sparking then return end;

	local effectdata = EffectData();
	effectdata:SetOrigin( self:GetPos() );
	effectdata:SetMagnitude( 1 );
	effectdata:SetScale( 1 );
	effectdata:SetRadius( 2 );
	util.Effect( "Sparks", effectdata );
end;

Here is the error it gives me


[ERROR] addons/darkrpmodification-master/lua/entities/k_printer_t8/init.lua:149: attempt to index global 'ent' (a nil value)
  1. unknown - addons/darkrpmodification-master/lua/entities/k_printer_t8/init.lua:149


use self instead of ent?

Still nothing :confused:

I think you’ll actually want to put it in Ent:Draw(), if I’m correct.
What are you doing to set the color of the printer?

I do not have a ENT:Draw() function.


local function SetValues( ent )
	ent.printTime = 10; -- Default print time.
	ent.minPrint = 5; -- Minimum print amount.
	ent.maxPrint = 2000; -- Maximum print amount.
	ent.upgradedExtra = ent.maxPrint * 0.5; -- The additional income received on upgraded printers.
	if CurTime() >= ent.nextcolorchange then
    ent.nextcolorchange = CurTime()+3
    ent.printerColor = Color(math.random(0,255),math.random(0,255),math.random(0,255), 255 );
	end
	ent.coolantSystem = false; -- Toggles the coolant system.
	ent.coolantLoss = 0.5; -- The Percentage loss for each print of the coolant is enabled.
end;

local PrintMore;
function ENT:Initialize()
	self:SetModel( "models/props_lab/reciever01a.mdl" );
	self:PhysicsInit( SOLID_VPHYSICS );
	self:SetMoveType( MOVETYPE_VPHYSICS );
	self:SetSolid( SOLID_VPHYSICS );
	local phys = self:GetPhysicsObject();
	phys:Wake();

	SetValues( self );
	
	self:SetColor( self.printerColor );
	self.damage = 500;
	self.IsMoneyPrinter = true;
	self:SetNWInt( "Amount", 0 );
	self:SetNWInt( "Health", 500 );
	self:SetNWBool( "Upgraded", false );
	if self.coolantSystem then
		self:SetNWInt( "Coolant", 100 );
		self:SetNWBool( "CoolantToggle", true );
	end;
	timer.Simple( 0.1, function() PrintMore( self ) end );
end;

The above code is the portion of the code that sets the color of the printer.

Try this


local function SetValues( ent )
        ent.nextcolorchange = CurTime()+3
	ent.printTime = 10; -- Default print time.
	ent.minPrint = 5; -- Minimum print amount.
	ent.maxPrint = 2000; -- Maximum print amount.
	ent.upgradedExtra = ent.maxPrint * 0.5; -- The additional income received on upgraded printers.
	if CurTime() >= ent.nextcolorchange then
    ent.printerColor = Color(math.random(0,255),math.random(0,255),math.random(0,255), 255 );
	end
	ent.coolantSystem = false; -- Toggles the coolant system.
	ent.coolantLoss = 0.5; -- The Percentage loss for each print of the coolant is enabled.
end;

local PrintMore;
function ENT:Initialize()
	self:SetModel( "models/props_lab/reciever01a.mdl" );
	self:PhysicsInit( SOLID_VPHYSICS );
	self:SetMoveType( MOVETYPE_VPHYSICS );
	self:SetSolid( SOLID_VPHYSICS );
	local phys = self:GetPhysicsObject();
	phys:Wake();

	SetValues( self );
	
	self:SetColor( self.printerColor );
	self.damage = 500;
	self.IsMoneyPrinter = true;
	self:SetNWInt( "Amount", 0 );
	self:SetNWInt( "Health", 500 );
	self:SetNWBool( "Upgraded", false );
	if self.coolantSystem then
		self:SetNWInt( "Coolant", 100 );
		self:SetNWBool( "CoolantToggle", true );
	end;
	timer.Simple( 0.1, function() PrintMore( self ) end );
end;

As I have already told him, and if you read the thread, SetValues is only called once. That will not work.

It only changes the color of the printer in ENT:Initialize(), which is only called when the entity initalizes…
You aren’t actually setting the color of the printer, you are just setting a variable to a color and it won’t do anything because it uses self.printerColor in ENT:Initialize().

So as I said before, put all of that into ENT:think(), change ent. to self. and use self:SetColor() instead of assigning a color to self.printercolor.