• Money printer (money inside)
    7 replies, posted
Hello peaople, I lua newbie, could you help me with a problem I'm trying to do with the money inside the printer, the essence is this: with spawn printer he has five One unit of energy, every N seconds, the energy is reduced and the money is increasing, almost everything is produced, but the problem is that if I create a second printer gets off the counter (returned by 5) in both how to do it were specific for each? P.S. This is to ensure that each player would be able to see how much power is left in each printer Codes: share.lua ENT.Type = "anim" ENT.Base = "base_gmodentity" ENT.PrintName = "Money Printer" ENT.Author = "Stanislav" ENT.Spawnable = false ENT.AdminSpawnable = false init.lua AddCSLuaFile("cl_init.lua") AddCSLuaFile("shared.lua") include("shared.lua") function ENT:printMoney() if self:GetNWInt("energy"..self:EntIndex())>0 then self:SetNWInt("energy"..self:EntIndex(),self:GetNWInt("energy"..self:EntIndex())-1) end end function ENT:Initialize() self:SetModel("models/als/flinter/fearless_money_printer.mdl") self:PhysicsInit(SOLID_VPHYSICS) self:SetMoveType(MOVETYPE_VPHYSICS) self:SetSolid(SOLID_VPHYSICS) local phys = self:GetPhysicsObject() phys:Wake() self.damage = 100 self:SetNWInt("energy"..self:EntIndex(),5) self.IsMoneyPrinter = true timer.Create("PMoney"..self:EntIndex(), 10, 0, function() self:printMoney() end) end function ENT:OnTakeDamage(dmg) if self.burningup then return end self.damage = (self.damage or 100) - dmg:GetDamage() if self.damage <= 0 then self:Destruct() self:Remove() end end function ENT:Destruct() local vPoint = self:GetPos() local effectdata = EffectData() effectdata:SetStart(vPoint) effectdata:SetOrigin(vPoint) effectdata:SetScale(1) util.Effect("Explosion", effectdata) timer.Destroy("PMoney"..self:EntIndex()) end function ENT:Think() 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 cl_init.lua include("shared.lua") function ENT:Initialize() end function ENT:Think() hook.Add( "HUDPaint", "BG1", function() Aim=util.TraceLine( util.GetPlayerTrace( LocalPlayer() ) ).Entity if IsValid(Aim) then if Aim:GetClass() == "money_printer" then draw.SimpleText("Money printer","TargetID", ScrW()/2,ScrH()/2-20, Color(0,255,40,200), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER) draw.SimpleText("Energy: "..self:GetNWInt("energy"..self:EntIndex()),"TargetID", ScrW()/2,ScrH()/2, Color(0,255,40,200), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER) draw.SimpleText("Cash: "..((5-self:GetNWInt("energy"..self:EntIndex()))*300).."$","TargetID", ScrW()/2,ScrH()/2+20, Color(0,255,40,200), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER) else return false end end end) end
please use lua tags
Shit, you're adding a hook every frame?
[lua] ENT.Type = "anim" ENT.Base = "base_gmodentity" ENT.PrintName = "Money Printer" ENT.Author = "" ENT.Spawnable = false ENT.AdminSpawnable = false if SERVER then AddCSLuaFile("shared.lua") function ENT:PrintMoney() local CurrentEnergy = self:GetNWInt("energy") local CurrentMoney = self:GetNWInt("money") if CurrentEnergy > 0 then self:SetNWInt("energy", CurrentEnergy - self.EnergyReduction) self:SetNWInt("money", CurrentMoney + self.MoneyGain) end end function ENT:Initialize() -- self:SetModel("models/als/flinter/fearless_money_printer.mdl") self:SetModel("models/props_c17/FurnitureWashingmachine001a.mdl") self:PhysicsInit(SOLID_VPHYSICS) self:SetMoveType(MOVETYPE_VPHYSICS) self:SetSolid(SOLID_VPHYSICS) self:SetUseType(SIMPLE_USE) local phys = self:GetPhysicsObject() phys:Wake() self.Time = CurTime() self.PrintDelay = 10 self.Hp = 100 self.Energy = 5 self.EnergyReduction = 1 self.MoneyGain = 100 self.StartingMoney = 0 self:SetNWInt("energy", self.Energy) self:SetNWInt("money", self.StartingMoney) end function ENT:OnTakeDamage(dmg) self.Hp = self.Hp - dmg:GetDamage() if self.Hp <= 0 then self:Destruct() end end function ENT:Destruct() local vPoint = self:GetPos() local effectdata = EffectData() effectdata:SetStart(vPoint) effectdata:SetOrigin(vPoint) effectdata:SetScale(1) util.Effect("Explosion", effectdata) self:Remove() end function ENT:Use(activator, caller) if (activator:IsPlayer()) then activator:AddMoney(self:GetNWInt("money")) self:SetNWInt("money", 0) end end function ENT:Think() if self.Time + self.PrintDelay <= CurTime() then self.Time = CurTime() self:PrintMoney() end if self:WaterLevel() >= 1 then self:Destruct() return end end end if CLIENT then function ENT:Draw() self:DrawModel() end hook.Add( "HUDPaint", "BG1", function() local ply = LocalPlayer() local Trace = ply:GetEyeTrace().Entity if IsValid(Trace) then if (Trace:GetClass() == "money_printer") and (ply:GetPos():Distance(Trace:GetPos()) < 150) then draw.SimpleTextOutlined(Trace.PrintName, "DermaDefault", ScrW()/2, (ScrH()/2)-20, Color(0,255,40,200), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER, 1, Color(0,0,0,255)) draw.SimpleTextOutlined("Energy: "..Trace:GetNWInt("energy"),"DermaDefault", ScrW()/2, ScrH()/2, Color(0,255,40,200), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER, 1, Color(0,0,0,255)) draw.SimpleTextOutlined("Cash: $"..Trace:GetNWInt("money"),"DermaDefault", ScrW()/2, (ScrH()/2)+20, Color(0,255,40,200), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER, 1, Color(0,0,0,255)) end end end) end [/lua] There you go twoski fixed it up. Place in shared.lua and enjoy.
SweetTea, sorry, Next time required DaneSomdahl, brandonj4, ty very much)
this is hideous, you should fix it before you consider using it. you're using timers to do what a think hook should be doing. you don't need to use EntIndex in your networked value names. is this really the code behind DarkRP? because damn son. Also it wouldn't hurt to keep this separated into cl_init, shared and init, it makes things less confusing for beginners and keeps the code semi-tidy
[QUOTE=twoski;38310188]this is hideous, you should fix it before you consider using it. you're using timers to do what a think hook should be doing. you don't need to use EntIndex in your networked value names. is this really the code behind DarkRP? because damn son. Also it wouldn't hurt to keep this separated into cl_init, shared and init, it makes things less confusing for beginners and keeps the code semi-tidy[/QUOTE] -snip- It never needed to be fixed.
You're missing a validity check for the physics object in ENT:Initialize but yes that's better.
Sorry, you need to Log In to post a reply to this thread.