• net.SendToServer nil value
    3 replies, posted
This is the code I am calling clientside to send the server the integer. [code] AddCSLuaFile("cl_init.lua") AddCSLuaFile("shared.lua") include('shared.lua') if SERVER then ENT.cratesAdded = 0 end function ENT:Initialize() self:SetModel("models/Items/item_item_crate.mdl") self:PhysicsInit(SOLID_VPHYSICS) self:SetMoveType(MOVETYPE_VPHYSICS) self:SetSolid(SOLID_VPHYSICS) self:SetUseType(SIMPLE_USE) local phys = self:GetPhysicsObject() if phys:IsValid() then phys:Wake() end end function ENT:Touch(ent) if ent.IsVehicle() then self:EmitSound( "items/ammocrate_close.wav", 75, 100, 1, CHAN_AUTO ) self:Remove() print("crate added") net.Start( "cratesAdd" ) net.WriteUInt( self.cratesAdded + 1, 8 ) -- You can also use net.WriteInt here. The second argument is 8 since the age will never be above 255. Doing this saves bandwidth and can reduce lag. net.SendToServer() end end [/code] and my serverside which tries to get the cratesAdded [code] util.AddNetworkString( "cratesAdd" ) net.Receive( "cratesAdd", function() print("Received message! You have added "..cratesAdded.. " crates!") end) [/code] and this is the error I get.. [code] [ERROR] addons/citizenjobs/lua/entities/crate/init.lua:19: attempt to call field 'SendToServer' (a nil value) 1. unknown - addons/citizenjobs/lua/entities/crate/init.lua:19 [/code]
[URL="http://wiki.garrysmod.com/page/ENTITY/Touch"]ENT:Touch()[/URL] is a serverside only callback. [URL="http://wiki.garrysmod.com/page/net/SendToServer"]net.SendToServer()[/URL] is clientside only so the server doesn't have it. You don't even have to send anything from client to server since the server knows it touched by itself.
[QUOTE=syl0r;49796959][URL="http://wiki.garrysmod.com/page/ENTITY/Touch"]ENT:Touch()[/URL] is a serverside only callback. [URL="http://wiki.garrysmod.com/page/net/SendToServer"]net.SendToServer()[/URL] is clientside only so the server doesn't have it. You don't even have to send anything from client to server since the server knows it touched by itself.[/QUOTE] Ahh I see where I went wrong. I am calling net.SendToServer on serverside so its trying to send it to itself. Should I use broadcast instead?
[QUOTE=AustinH;49796971]Ahh I see where I went wrong. I am calling net.SendToServer on serverside so its trying to send it to itself. Should I use broadcast instead?[/QUOTE] no don't use net library at all, just a simple function call [lua]function ENT:Touch(ent) if ent.IsVehicle() then self:EmitSound( "items/ammocrate_close.wav", 75, 100, 1, CHAN_AUTO ) self:Remove() print("crate added") addCrate() -- or whatever end end[/lua]
Sorry, you need to Log In to post a reply to this thread.