• Random money printer color every 3 seconds
    21 replies, posted
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: [CODE]ent.printerColor = Color(math.random(0,255),math.random(0,255),math.random(0,255), 255 );[/CODE]
You could use a loop.
[QUOTE=Cosmo51;50062754]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: [CODE]ent.printerColor = Color(math.random(0,255),math.random(0,255),math.random(0,255), 255 );[/CODE][/QUOTE] When the entity is create you would one to run a timer to set the color at what ever time frame you want. [QUOTE=dannyf127;50064052]You could use a loop.[/QUOTE] No, JUST NO!
[CODE] 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) [/CODE] 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]
[QUOTE=Neat-Nit;50065252]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][/QUOTE] Oh ok. Thanks from myself, I'll keep something like that in mind for a next time :)
[QUOTE=Neat-Nit;50065252]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][/QUOTE] This is what I have [CODE]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;[/CODE] This is the error it gives me [CODE] [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 [/CODE]
You didn't define ent.nextcolorchange in the first place
[QUOTE=LittleBigBug;50070573]You didn't define ent.nextcolorchange in the first place[/QUOTE] I thought this was defining ent.nextcolorchange ? Sorry man, I'm a lua noob haha. [CODE]ent.nextcolorchange = CurTime()+3[/CODE]
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. [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 [/CODE]
[QUOTE=Neat-Nit;50071700]Hint: the first time this code runs, ent.nextcolorchange is nil. In that case you should probably set it to 0 before doing anything.[/QUOTE] I changed my code to this. [CODE]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;[/CODE] This makes the printer spawn with a random color, but the color wont change. I tried this aswell, [CODE]ent.nextcolorchange = math.random(0,255)[/CODE] 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.
[QUOTE=Cosmo51;50075409]I changed my code to this. [CODE]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;[/CODE] This makes the printer spawn with a random color, but the color wont change. I tried this aswell, [CODE]ent.nextcolorchange = math.random(0,255)[/CODE] 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.[/QUOTE] 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()
[QUOTE=Nick78111;50075851]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()[/QUOTE] Here is my ENT:Think [CODE]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;[/CODE] Here is the error it gives me [CODE][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 [/CODE]
use self instead of ent?
[QUOTE=Invule;50076470]use self instead of ent?[/QUOTE] Still nothing :/
[QUOTE=Cosmo51;50076516]Still nothing :/[/QUOTE] 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?
[QUOTE=LittleBigBug;50076978]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?[/QUOTE] I do not have a ENT:Draw() function. [CODE]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;[/CODE] The above code is the portion of the code that sets the color of the printer.
Try this [CODE]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;[/CODE]
[QUOTE=dannyf127;50079047]Try this [CODE]non working code[/CODE][/QUOTE] 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.
Oh for crying out loud. Put this in Think: [lua]self.nextcolorchange = self.nextcolorchange or CurTime()+3 if CurTime() >= self.nextcolorchange then self.nextcolorchange = CurTime()+3 self.printerColor = -- your random color here self:SetColor(self.printerColor) -- I assume this affects the printing color like you want end[/lua]
[QUOTE=Neat-Nit;50079799]Oh for crying out loud. Put this in Think: [lua]self.nextcolorchange = self.nextcolorchange or CurTime()+3 if CurTime() >= self.nextcolorchange then self.nextcolorchange = CurTime()+3 self.printerColor = -- your random color here self:SetColor(self.printerColor) -- I assume this affects the printing color like you want end[/lua][/QUOTE] Thanks man, again sorry for bothering you. +1
Sorry, you need to Log In to post a reply to this thread.