so I moved the checks, and subtraction serverside, to its own function like this
[CODE]function scrapchecking()
if self.ScrapNumber < 3 then
ply:ChatPrint( "You Do Not Have Enough scrap" )
elseif
self.ScrapNumber >= 3 then
spawncbar()
self.ScrapNumber = self.ScrapNumber - 3
self.SetNWInt("self.NewScrapNumber", self.ScrapNumber)
ply:ChatPrint( "Congratz you've got a crowbar" )
return
self.ScrapNumber
end
end
[/CODE]
How would I call this function on the client side, when I click the button.
I am also getting this error,[QUOTE]addons/crafting/lua/entities/melter/cl_init.lua:60: attempt to index local 'self' (a nil value)
1. Function - addons/crafting/lua/entities/melter/cl_init.lua:60
2. unknown - lua/includes/modules/usermessage.lua:87
[/QUOTE]
my line 60
[CODE]scrapnumber:SetText(self:GetNWInt("self.NewScrapNumber"))[/CODE]
because self is undefined. You could send the entity in the usermessage, and retrieve it using self = data:ReadEntity() where data is the first argument of the function. You would call the checks serverside within the net callback serverside.
So I added [CODE]umsg.Entity("melter")[/CODE] in the usermessage.
then I did [CODE]self = data:ReadEntity()[/CODE], and made data the first argument of the function, but now i get this [QUOTE]
[ERROR] addons/crafting/lua/entities/melter/cl_init.lua:59: attempt to index local 'self' (a nil value)
1. Function - addons/crafting/lua/entities/melter/cl_init.lua:59
2. unknown - lua/includes/modules/usermessage.lua:87
[/QUOTE]
can you post cl_init.lua ? Also I'm tired so I didn't even notice: You're writing a string, not an entity.
Are you even writing the entity? I think that you're doing it in the most hard way
cl_init.lua
[CODE]
include("shared.lua")
function ENT:Initialize()
end
function ENT:Draw()
self:DrawModel()
local Pos = self:GetPos()
local Ang = self:GetAngles()
owner = (IsValid(owner) and owner:Nick())
local text = Melter
Ang:RotateAroundAxis(Ang:Up(), 90)
end
function ENT:Think()
end
function SmeltingVgui( data, smelter)
local ply = LocalPlayer()
local sh = (400)
local sw = (300)
local self = data:ReadEntity()
local frame = vgui.Create("DFrame")
frame:SetPos(sh,sw)
frame:SetSize(sh,sw)
frame:SetTitle("Smelter")
frame:SetVisible(true)
frame:SetDraggable(false)
frame:ShowCloseButton(true)
frame:MakePopup()
local CBarButton = vgui.Create("DButton", frame) --CrowBarr weapon derma button
CBarButton:SetPos(80,80)
CBarButton:SetText("crowbarz")
CBarButton:SetSize(40,40)
CBarButton.DoClick = function()
end
local scrapnumber = vgui.Create("DLabel", frame) --Amount of scrap. Dermal Lable
scrapnumber:SetPos(40,40)
scrapnumber:SetText(self:GetNWInt("self.NewScrapNumber"))
end
usermessage.Hook("smelter", SmeltingVgui)
[/CODE]
init.lua
[CODE]
AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
include("shared.lua")
function ENT:Initialize()
self:SetModel("models/props_c17/furnitureStove001a.mdl")
self:PhysicsInit(SOLID_VPHYSICS)
self:SetMoveType(MOVETYPE_VPHYSICS)
self:SetSolid(SOLID_VPHYSICS)
local phys = self:GetPhysicsObject()
phys:Wake()
self.ScrapNumber = 0
self:SetNWInt("self.ScrapNumber", self.ScrapNumber)
self:SetUseType( SIMPLE_USE )
end
function ENT:Touch( entity )
if entity:GetClass() == "scrap" then
print( "Working?", self, entity )
self.ScrapNumber = self.ScrapNumber + 1
self:SetNWInt("self.ScrapNumber", self.ScrapNumber)
entity:Remove()
end
end
function ENT:AcceptInput(Name, Activator, Caller) -- when player press's the use key on the entity
if Name == "Use" and Caller:IsPlayer() then
umsg.Start("smelter", Caller)
umsg.Entity("melter")
umsg.End()
end
end
util.AddNetworkString( "ScrapCbar" )
net.Receive( "ScrapCbar", function( length, client, self ) -- receiving the varrible from client, in client when ever the Dbutton is pushed
local data = net.ReadString()
if data == "ScrapCbar" then
spawncbar(client:GetEyeTraceNoCursor().HitPos+Vector(0,0,30))
NewScrapAmount = self:GetNWInt("self.NewScrapNumber", self.ScrapNumber)
self:SetNWInt("self.ScrapNumber", self.ScrapNumber)
end
end)
function spawncbar() -- The HL2 crowbar weapon
Cbwep = ents.Create( "weapon_crowbar" )
Cbwep:SetPos(10,10,10)
Cbwep:Spawn()
end
function scrapchecking()
if self.ScrapNumber < 3 then
ply:ChatPrint( "You Do Not Have Enough scrap" )
elseif
self.ScrapNumber >= 3 then
spawncbar()
self.ScrapNumber = self.ScrapNumber - 3
self.SetNWInt("self.NewScrapNumber", self.ScrapNumber)
ply:ChatPrint( "Congratz you've got a crowbar" )
return
self.ScrapNumber
end
end
[/CODE]
You've sent a string not an entity. As I said in my previous post. usmg.WriteEntity takes an entity as an argument, not a string, as shown on the wiki.
[QUOTE=James xX;47564870]You've sent a string not an entity. As I said in my previous post. usmg.WriteEntity takes an entity as an argument, not a string, as shown on the wiki.[/QUOTE]
There's no usmg.WriteEntity() on the wiki, but what I did find was usmg.Entity() and net.WriteEntity(), so that would mean that I should switch from using usermessage and use the net-library,
so in the init.lua should I change
[CODE]function ENT:AcceptInput(Name, Activator, Caller) -- when player press's the use key on the entity
if Name == "Use" and Caller:IsPlayer() then
umsg.Start("smelter", Caller)
umsg.Entity("melter")
umsg.End()
end
end[/CODE]
to this
[CODE]util.AddNetworkString("SmelterVgui")
function ENT:AcceptInput(Name, Activator, Caller) -- when player press's the use key on the entity
if Name == "Use" and Caller:IsPlayer() then
net.start("SmelterVgui")
net.WriteEntity("melter")
net.end()
end
end[/CODE]
and then would I receive it on the cl_init like this
[CODE]net.Receive( "SmelterVgui", function ( data, len)
local ply = LocalPlayer()
local sh = (400)
local sw = (300)
local self = data:ReadEntity()
local frame = vgui.Create("DFrame")
frame:SetPos(sh,sw)
frame:SetSize(sh,sw)
frame:SetTitle("Smelter")
frame:SetVisible(true)
frame:SetDraggable(false)
frame:ShowCloseButton(true)
frame:MakePopup()
local CBarButton = vgui.Create("DButton", frame) --CrowBarr weapon derma button
CBarButton:SetPos(80,80)
CBarButton:SetText("crowbarz")
CBarButton:SetSize(40,40)
CBarButton.DoClick = function()
end
local scrapnumber = vgui.Create("DLabel", frame) --Amount of scrap. Dermal Lable
scrapnumber:SetPos(40,40)
scrapnumber:SetText(self:GetNWInt("self.NewScrapNumber"))
end)[/CODE]
[QUOTE=funnygamemake;47568599]There's no usmg.WriteEntity() on the wiki, but what I did find was usmg.Entity() and net.WriteEntity(), so that would mean that I should switch from using usermessage and use the net-library,
so in the init.lua should I change
[CODE]function ENT:AcceptInput(Name, Activator, Caller) -- when player press's the use key on the entity
if Name == "Use" and Caller:IsPlayer() then
umsg.Start("smelter", Caller)
umsg.Entity("melter")
umsg.End()
end
end[/CODE]
to this
[CODE]util.AddNetworkString("SmelterVgui")
function ENT:AcceptInput(Name, Activator, Caller) -- when player press's the use key on the entity
if Name == "Use" and Caller:IsPlayer() then
net.start("SmelterVgui")
net.WriteEntity("melter")
net.end()
end
end[/CODE]
and then would I receive it on the cl_init like this
[CODE]net.Receive( "SmelterVgui", function ( data, len)
local ply = LocalPlayer()
local sh = (400)
local sw = (300)
local self = data:ReadEntity()
local frame = vgui.Create("DFrame")
frame:SetPos(sh,sw)
frame:SetSize(sh,sw)
frame:SetTitle("Smelter")
frame:SetVisible(true)
frame:SetDraggable(false)
frame:ShowCloseButton(true)
frame:MakePopup()
local CBarButton = vgui.Create("DButton", frame) --CrowBarr weapon derma button
CBarButton:SetPos(80,80)
CBarButton:SetText("crowbarz")
CBarButton:SetSize(40,40)
CBarButton.DoClick = function()
end
local scrapnumber = vgui.Create("DLabel", frame) --Amount of scrap. Dermal Lable
scrapnumber:SetPos(40,40)
scrapnumber:SetText(self:GetNWInt("self.NewScrapNumber"))
end)[/CODE][/QUOTE]
It really doesn't matter which system you use, if when you call the function to send an entity, you send a string, because it won't work.
[QUOTE=James xX;47568669]It really doesn't matter which system you use, if when you call the function to send an entity, you send a string, because it won't work.[/QUOTE]
But what I currently change, is that sending the entity, or is it still sending a string
[QUOTE=funnygamemake;47568696]But what I currently change, is that sending the entity, or is it still sending a string[/QUOTE]
It's still a string, as I said.
[QUOTE=James xX;47568706]It's still a string, as I said.[/QUOTE]
then what would I do to make it write it as an entity, because I was Using the net.WriteEntity(), or Umsg.Entity(). So what would I do to write it as a entity, I'm sorry for any stupid question that I am asking, but I clearly dont understand this part.
It doesn't matter which function you use, as long as you are consistent with it. If you use net, it has to be within a net.Start and net.Send. if you are using usmg, it has to be between usmg.Start and you get the idea. If you use net server side, you have to use it clientside. You can't mix and match.
net.WriteEntity( self ) is what you should use (or the usmg equivalent). No quotes.
I'm now working on a different part of the script and I'm getting [QUOTE]attempt to index local data (a nil value)[/QUOTE]
I know this is just like the self thing, but I don't know how I'm suppose to send the argument data in a networkstring. I know the problem, the problem is that when the script dose net.recieve and runs the function it dost have and value for data, so what I am asking is, how would I send the argument data over to the net.recieve function.
init.lua
[CODE]util.AddNetworkString("ScrapChecking")
net.Receive("ScrapChecking", scrapchecking)
function scrapchecking(len, pl, data, self)
print('test')
local self = data:ReadEntity() -- it is not reading, or it has no current value for data
if self.ScrapNumber < 3 then
pl:ChatPrint( "You Do Not Have Enough scrap" )
elseif
self.ScrapNumber >= 3 then
spawncbar()
self.ScrapNumber = self.ScrapNumber - 3
self.SetNWInt("self.ScrapNumber", self.ScrapNumber)
pl:ChatPrint( "Congratz you've got a crowbar" )
return
self.ScrapNumber
end
end[/CODE]
cl_init.lua
[CODE] local CBarButton = vgui.Create("DButton", frame) --CrowBarr weapon derma button
CBarButton:SetPos(80,80)
CBarButton:SetText("crowbarz")
CBarButton:SetSize(40,40)
CBarButton.DoClick = function(data, self)
net.Start("ScrapChecking")
net.WriteEntity(self)
net.SendToServer()
end[/CODE]
Another problem that I have is, when the scrap touches the entity it dost just add one it duplicates(example, i want the self.ScrapNumber to just go up bye one every time, but instead it likes to go like, 2,4,8,..etc..), how would I solve this. I'm guessing its an error here.
[CODE]function ENT:Touch( entity )
if entity:GetClass() == "scrap" then
print( "Working?", self, entity )
self.ScrapNumber = self.ScrapNumber + 1
self:SetNWInt("self.ScrapNumber", self.ScrapNumber)
entity:Remove()
end
end[/CODE]
Can't add you on steam until you unignore me. Your problem is you are mixing up usermessages with net messages. You are also trying to use a function before it is being created.
[QUOTE=James xX;47574776]Can't add you on steam until you unignore me. Your problem is you are mixing up user-messages with net messages. You are also trying to use a function before it is being created.[/QUOTE] Would you happen to know the problem with the self.ScrapNumber not going up by one every time it touch's.
Does it print? Also, I still can't add you on steam.
[QUOTE=James xX;47576707]Does it print? Also, I still can't add you on steam.[/QUOTE]
How do I unignore you, cause theres no option that I can find, or ill add you, whats your username