I was just curious as to how about's I would have a derma button run a server side operation.
E.G: When you press use on a money printer, a menu comes up and you can press 'Get Money' which will then give you the money currently stored in an nwint.
Any help would be appreciated.
Console command or the net library.
I was thinking of using console command, how ever I don't want the command to be available other then from the DButton.
Net library 'looks' quite confusing, but definitely willing to learn to use it.
To put it blatantly; where would I start?
This is a test file I made to check if it works
[lua]if CLIENT then
--clientside code
--call the panel however you want clientside
local DermaPanel = vgui.Create( "DFrame" )
DermaPanel:SetPos( 100,75 )
DermaPanel:SetSize( 200, 75 )
DermaPanel:SetTitle("money")
DermaPanel:SetVisible( true )
DermaPanel:SetDraggable( true )
DermaPanel:ShowCloseButton( true )
DermaPanel:MakePopup()
local DermaButton = vgui.Create( "DButton" )
DermaButton:SetParent( DermaPanel )
DermaButton:SetText( "give money" )
DermaButton:SetPos( 10, 30 )
DermaButton:SetSize( 100, 30 )
DermaButton.DoClick = function ()
local money = math.random( 100, 500 ) --replace with how you get money from the printer
net.Start("send_money")
net.WriteInt(money, 16) --Is 16 bits enough?
net.WriteEntity(LocalPlayer())
net.SendToServer()
LocalPlayer():PrintMessage( HUD_PRINTTALK , "you got $" .. money .. " from this printer")
end
else
--server side code
util.AddNetworkString("send_money") --call before net.start
ply:SetNWInt("money", 0) --i did this to start off with $0
function giveplayermoney(ply, money)
ply:SetNWInt("money", ply:GetNWInt("money") + money) --replace with however you set/give money
print(ply:GetNWInt("money")) --test to see if it worked
end
function getmoneyfromplayer()
net.Receive( "send_money", function( len )
local money = net.ReadInt(16)
local ply = net.ReadEntity()
giveplayermoney(ply, money)
end)
end
getmoneyfromplayer()
end[/lua]
and output
[CODE]>lua_openscript moneytest.lua
>>Running script moneytest.lua...
> lua_openscript_cl moneytest.lua
>Running script moneytest.lua...
>>you got $215 from this printer
>>215
[/CODE]
so look at what i did and make it work for you
Except, you know, don't let the client decide how much money their printer is giving them.
[QUOTE=Virulity;38258890]Except, you know, don't let the client decide how much money their printer is giving them.[/QUOTE]
ofc
I can understand how you've done that and I do appreciate it maxb130, only ridiculous problem (on my lack of understanding) is calling self:GetNWInt("money_printer_money") to get how much the printer has stored, that way I can send that amount.
post some code, so we can better help you
You need to make sure that the server-side for when they get the money that it's a printer entity or it can be exploited to get infinite money.
init.lua
[lua]
AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
include("shared.lua")
ENT.SeizeReward = 950
local RecieveMoney
function ENT:Initialize()
self:SetModel("models/props_c17/consolebox01a.mdl")
self:PhysicsInit(SOLID_VPHYSICS)
self:SetMoveType(MOVETYPE_VPHYSICS)
self:SetSolid(SOLID_VPHYSICS)
local phys = self:GetPhysicsObject()
phys:Wake()
self.sparking = false
self.damage = 100
self.HasCooler = nil
timer.Simple(math.random(5, 10), function() RecieveMoney(self) end)
self:SetNWInt("hackermoney", 0)
end
function ENT:OnTakeDamage(dmg)
if self.burningup then return end
self.damage = (self.damage or 100) - dmg:GetDamage()
if self.damage <= 0 then
local rnd = math.random(1, 10)
if rnd < 3 then
self:BurstIntoFlames()
else
self:Destruct()
self:Remove()
end
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)
end
function ENT:Fireball()
if not self:IsOnFire() then self.burningup = false return end
local dist = math.random(20, 280) -- Explosion radius
self:Destruct()
for k, v in pairs(ents.FindInSphere(self:GetPos(), dist)) do
if not v:IsPlayer() and not v:IsWeapon() and v:GetClass() ~= "predicted_viewmodel" and not v.IsMoneyPrinter then
v:Ignite(math.random(5, 22), 0)
elseif v:IsPlayer() then
local distance = v:GetPos():Distance(self:GetPos())
v:TakeDamage(distance / dist * 100, self, self)
end
end
self:Remove()
end
function ENT:BurstIntoFlames()
self.burningup = true
local burntime = math.random(8, 18)
self:Ignite(burntime, 0)
timer.Simple(burntime, function() self:Fireball() end)
end
function ENT:RecieveMoneyInt()
if not IsValid(self) then return end
if self:IsOnFire() then return end
if math.random(1, 22) == 3 then self:BurstIntoFlames() end
local amount = 75
self:SetNWInt("printmoney", self:GetNWInt("printmoney") + amount)
self.sparking = false
timer.Simple(math.random(5, 10), function() RecieveMoney(self) end)
end
RecieveMoney = function(ent)
if IsValid(ent) then
ent.sparking = true
timer.Simple(3, function() ent:RecieveMoneyInt() end)
end
end
function ENT:Use(caller)
umsg.Start("printer_menu", caller)
umsg.End()
end
util.AddNetworkString("collectfunds")
function CollectPrinterFunds(ply)
ply:AddMoney(self:GetNWInt("printmoney"))
print(ply:GetNWInt("printmoney"))
end
function CollectPrinterFundsClient()
net.Receive("collectfunds", function(len)
local amount = self:GetNWInt("printmoney")
local ply = net.ReadEntity()
CollectPrinterFunds(ply)
end)
end
CollectPrinterFundsClient()
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
[/lua]
cl_init.lua
[lua]
include("shared.lua")
function ENT:Initialize()
end
function ENT:Draw()
self:DrawModel()
local Ang = self:GetAngles()
local Pos = self:GetPos()
local owner = self.dt.owning_ent
owner = (IsValid(owner) and owner:Nick()) or "unknown"
surface.SetFont("HUDNumber5")
local TextWidth = surface.GetTextSize("Printer")
local TextWidth2 = surface.GetTextSize(owner)
Ang:RotateAroundAxis(Ang:Up(), 0)
cam.Start3D2D(Pos + Vector(0, -5.4, 0) + Ang:Up() * 10, Angle(0, Ang.y, 90), 0.076)
draw.WordBox(2, -TextWidth + 130, -90, "$"..self:GetNWInt("printermoney")..".00", "HUDNumber5", Color(0, 0, 0, 100), Color(255,255,255,255))
draw.WordBox(2, -TextWidth2*0.5, 18, owner, "HUDNumber5", Color(140, 0, 0, 100), Color(255,255,255,255))
cam.End3D2D()
end
function PrinterMenu()
if (dframe) then
dframe:Remove()
dframe = nil
end
dframe = vgui.Create("DFrame")
dframe:SetPos(ScrW()/2-125, ScrH()/2-125)
dframe:SetSize(250, 250)
dframe:SetTitle("Menu")
dframe:ShowCloseButton(true)
dframe:SetDraggable(false)
dframe:MakePopup()
dcollect = vgui.Create("DButton", dframe)
dcollect:SetPos(5, 28)
dcollect:SetSize(dframe:GetWide()-10, 25)
dcollect:SetText("Collect Funds")
dcollect.DoClick = function()
local money = self:GetNWInt("printmoney")
net.Start("collectfunds")
net.Write(money, 16)
net.WriteEntity(LocalPlayer())
net.SendToServer()
end
dupgrade = vgui.Create("DButton", dframe)
dupgrade:SetPos(5, 58)
dupgrade:SetSize(dframe:GetWide()-10, 25)
dupgrade:SetText("Implement Cooling System")
dupgrade.DoClick = function()
end
end
usermessage.Hook("printer_menu", PrinterMenu)
function ENT:Think()
end
[/lua]
I fixed your problem as well as some others that were there, check the comments to see what I did...
Tested and is working 100%
init.lua
[lua]
AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
include("shared.lua")
util.AddNetworkString("collectfunds") --moved to top of file
ENT.SeizeReward = 950
local RecieveMoney
function ENT:Initialize()
self:SetModel("models/props_c17/consolebox01a.mdl")
self:PhysicsInit(SOLID_VPHYSICS)
self:SetMoveType(MOVETYPE_VPHYSICS)
self:SetSolid(SOLID_VPHYSICS)
local phys = self:GetPhysicsObject()
phys:Wake()
self.sparking = false
self.damage = 100
self.HasCooler = nil
timer.Simple(math.random(5, 10), function() RecieveMoney(self) end)
self:SetNWInt("hackermoney", 0)
self:SetNWInt("printmoney", 0) --set money to $0 when initialized
end
function ENT:OnTakeDamage(dmg)
if self.burningup then return end
self.damage = (self.damage or 100) - dmg:GetDamage()
if self.damage <= 0 then
local rnd = math.random(1, 10)
if rnd < 3 then
self:BurstIntoFlames()
else
self:Destruct()
self:Remove()
end
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)
end
function ENT:Fireball()
if not self:IsOnFire() then self.burningup = false return end
local dist = math.random(20, 280) -- Explosion radius
self:Destruct()
for k, v in pairs(ents.FindInSphere(self:GetPos(), dist)) do
if not v:IsPlayer() and not v:IsWeapon() and v:GetClass() ~= "predicted_viewmodel" and not v.IsMoneyPrinter then
v:Ignite(math.random(5, 22), 0)
elseif v:IsPlayer() then
local distance = v:GetPos():Distance(self:GetPos())
v:TakeDamage(distance / dist * 100, self, self)
end
end
self:Remove()
end
function ENT:BurstIntoFlames()
self.burningup = true
local burntime = math.random(8, 18)
self:Ignite(burntime, 0)
timer.Simple(burntime, function() self:Fireball() end)
end
function ENT:RecieveMoneyInt()
if not IsValid(self) then return end
if self:IsOnFire() then return end
if math.random(1, 22) == 3 then self:BurstIntoFlames() end
local amount = 75
self:SetNWInt("printmoney", self:GetNWInt("printmoney") + amount)
self.sparking = false
timer.Simple(math.random(5, 10), function() RecieveMoney(self) end)
end
RecieveMoney = function(ent)
if IsValid(ent) && !ent:IsOnFire() then --added '&& !ent:IsOnFire()' because it should not begin to add money if it is on fire, also causing errors
ent.sparking = true
timer.Simple(3, function() ent:RecieveMoneyInt() end)
end
end
function CollectPrinterFunds(ply, ent)
ply:AddMoney(ent:GetNWInt("printmoney"))
ent:SetNWInt("printmoney", 0) --Remove the money you take out
end
function CollectPrinterFundsClient(ent)
net.Receive("collectfunds", function(len)
--do not get money here
local ply = net.ReadEntity()
CollectPrinterFunds(ply, ent) --pass the player and the entity
end)
end
function ENT:Use(caller) --moved below CollectPrinterFundsClient
if self:IsOnFire() then return end --cant remove money from burning printer... this also fixes a bug with removing money from printer and it not exploding
CollectPrinterFundsClient(self) --Pass Self
umsg.Start("printer_menu", caller)
umsg.End()
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
[/lua]
cl_init.lua
[lua]
include("shared.lua")
function ENT:Initialize()
end
function ENT:Draw()
self:DrawModel()
local Ang = self:GetAngles()
local Pos = self:GetPos()
local owner = self.dt.owning_ent
owner = (IsValid(owner) and owner:Nick()) or "unknown"
surface.SetFont("HUDNumber5")
local TextWidth = surface.GetTextSize("Printer")
local TextWidth2 = surface.GetTextSize(owner)
Ang:RotateAroundAxis(Ang:Up(), 0)
--cam.Start3D2D(Pos + Vector(0, -5.4, 0) + Ang:Up() * 10, Angle(0, Ang.y, 90), 0.076)
--draw.WordBox(2, -TextWidth + 130, -90, "$"..self:GetNWInt("printmoney")..".00", "HUDNumber5", Color(0, 0, 0, 100), Color(255,255,255,255))
----^ Was calling self:GetNWInt("printermoney") instead of self:GetNWInt("printmoney") above **fixed
--draw.WordBox(2, -TextWidth2*0.5, 18, owner, "HUDNumber5", Color(140, 0, 0, 100), Color(255,255,255,255))
--cam.End3D2D()
--*********I changed the cam.Start3D2D because it seemed weird the way you did it above, and it was printing text on the wrong axis
--*********feel free to comment the below out and use the above
cam.Start3D2D(Pos + Ang:Up() * 11.5, Ang, 0.11)
draw.WordBox(2, -TextWidth*0.5, -30, "$"..self:GetNWInt("printmoney")..".00", "HUDNumber5", Color(140, 0, 0, 100), Color(255,255,255,255))
--^ Was calling self:GetNWInt("printermoney") instead of self:GetNWInt("printmoney") above **fixed
draw.WordBox(2, -TextWidth2*0.5, 18, owner, "HUDNumber5", Color(140, 0, 0, 100), Color(255,255,255,255))
cam.End3D2D()
end
function PrinterMenu()
if (dframe) then
dframe:Remove()
dframe = nil
end
dframe = vgui.Create("DFrame")
dframe:SetPos(ScrW()/2-125, ScrH()/2-125)
dframe:SetSize(250, 250)
dframe:SetTitle("Menu")
dframe:ShowCloseButton(true)
dframe:SetDraggable(false)
dframe:MakePopup()
dcollect = vgui.Create("DButton", dframe)
dcollect:SetPos(5, 28)
dcollect:SetSize(dframe:GetWide()-10, 25)
dcollect:SetText("Collect Funds")
dcollect.DoClick = function()
--no need to try to get money from printer clientside
net.Start("collectfunds")
net.WriteEntity(LocalPlayer()) --just send who you are
net.SendToServer()
end
dupgrade = vgui.Create("DButton", dframe)
dupgrade:SetPos(5, 58)
dupgrade:SetSize(dframe:GetWide()-10, 25)
dupgrade:SetText("Implement Cooling System")
dupgrade.DoClick = function()
end
end
usermessage.Hook("printer_menu", PrinterMenu)
function ENT:Think()
end
[/lua]
Thank you for your time and explanations for all of the above.
Really appreciate what you changed and why it worked, thank you.
Regards,
NintendoEKS
Sorry, you need to Log In to post a reply to this thread.