Something causing errors in the net libary

Hi,
I tried to connect my server with my clients for my addon but when I add it to the server everything is messed up and nothing works anymore.
So that’s an evidence that the addon is causing it.

I looked into the console and before it is escalating in the console with errors it prints me this error
[LUA]bad argument #1 to ‘WriteInt’ (number expected, got nil) LINE 16 IS THE ERROR AFTER THE util.AddNetworkString(“passdata123”)![/LUA]
And the code of this, is this
[LUA]
if file.Exists(userDataFile, “DATA”) then
local playerData = util.JSONToTable(file.Read(userDataFile, “DATA”) )
TAmount = playerData.TAmount
DAmount = playerData.DAmount
util.AddNetworkString(“passdata123”)
net.Start(“passdata123”)
net.WriteInt(TAmount,8)
net.WriteInt(DAmount,8)
else
pass = {
TAmount = 0,
DAmount = 0,
steamid = ply:SteamID()
}
file.Write(userDataFile, util.TableToJSON(pass))
net.Start(“passdata123”)
net.WriteInt(TAmount,8)
net.WriteInt(DAmount,8)
end
[/LUA]
Something wrong connected? If player is new then a JSON table is beeing written in a .txt file.
If he joins and he already has that data then it loads it and I wrote
[LUA] TAmount = playerData.TAmount [/LUA]
because it is a lua table?

What is wrong with the code, maybe Im to blind to see it.

playerData.TAmount is nil, as the error suggests. It has no value.

There’s nothing stored in playerData.TAmount. There’s never a call like


 playerData.TAmount  = /*something*/

There definitly is, [LUA] {“DAmount”:0,“TAmount”:0,“steamid”:“STEAM_0:1:196861755”} [/LUA]

It still is 0, but its a number and he could send it to the player?

Oh I assumed the first net message is failing, but its the second.

You never set the variables TAmount and DAmount in your second net message.

Also you really should be using local variables for those two, not global, in your first net message.

Also you never send your net messages according to that code.

-snip-

Server
[LUA]
function Initialize()
if not file.IsDir( “pass”, “DATA” ) then
file.CreateDir(“pass”, “DATA” )
end
end
hook.Add(“Initialize”,“InitializePass”, Initialize)

function LoadData(ply)
local userDataFile = “pass/”…ply:UniqueID()…".txt"
if file.Exists(userDataFile, “DATA”) then
local playerData = util.JSONToTable(file.Read(userDataFile, “DATA”) )
local TAmount = playerData.TAmount
local DAmount = playerData.DAmount
util.AddNetworkString(“passdata123”)
net.Start(“passdata123”)
net.WriteInt(TAmount,8)
net.WriteInt(DAmount,8)
else
pass = {
TAmount = 0,
DAmount = 0,
steamid = ply:SteamID()
}
local userDataFile = “pass/”…ply:UniqueID()…".txt"
file.Write(userDataFile, util.TableToJSON(pass))
net.Start(“passdata123”)
net.WriteInt(pass.TAmount,8)
net.WriteInt(pass.DAmount,8)
end
end
hook.Add(“PlayerInitialSpawn”,“LoadPlayerData”,LoadData)

function SaveData(ply)
net.Receive(“savedata123”)
TAmount = net.ReadInt(8)
DAmount = net.ReadInt(8)
local userDataFile = “pass/”…ply:UniqueID()…".txt"
local pass = {
TAmount = TAmount,
DAmount = TAmount,
steamid = ply:SteamID()
}
file.Write(userDataFile, util.TableToJSON(pass))
end
hook.Add(“PlayerDisconnected”,“SavePlayerData”, SaveData)
hook.Add(“ShutDown”,“SavePlayerData2”,SaveData)
[/LUA]

CLIENT
[LUA]
hook.Add(“PlayerInitialSpawn”,“ReceiveDataPass”,function()
net.Receive(“passdata123”)
TAmount = net.ReadInt(8)
DAmount = net.ReadInt(8)
end)
// Bunch of codes

function sendBackData(ply)
util.AddNetworkString(“savedata123”)
net.Start(“savedata123”)
net.WriteInt(TAmount,8)
net.WriteInt(DAmount,8)
end
hook.Add(“PlayerDisconnected”,“sendBackData”,sendBackData)
hook.Add(“ShutDown”,“sendBackData2”,sendBackData)
[/LUA]

What does not work? My eyes still don’t see it.

Please read on how to use net.Receive ( wiki page should even have examples ) and how to send net message ( wiki has an article on it ) before asking questions

CLIENT
[LUA]
hook.Add(“PlayerInitialSpawn”,“ReceiveDataPass”,function()
net.Receive(“passdata123”, function(len,ply)
TAmount = net.ReadInt(8)
DAmount = net.ReadInt(8)
end)
end)

function sendBackData(ply)
util.AddNetworkString(“savedata123”)
net.Start(“savedata123”)
net.WriteInt(TAmount,8)
net.WriteInt(DAmount,8)
net.SendToServer()
end
hook.Add(“PlayerDisconnected”,“sendBackData”,sendBackData)
hook.Add(“ShutDown”,“sendBackData2”,sendBackData)

[/LUA]

SERVER
[LUA]
function Initialize()
if not file.IsDir( “pass”, “DATA” ) then
file.CreateDir(“pass”, “DATA” )
end
end
hook.Add(“Initialize”,“InitializePass”, Initialize)

function LoadData(ply)
local userDataFile = “pass/”…ply:UniqueID()…".txt"
if file.Exists(userDataFile, “DATA”) then
local playerData = util.JSONToTable(file.Read(userDataFile, “DATA”) )
local TAmount = playerData.TAmount
local DAmount = playerData.DAmount
util.AddNetworkString(“passdata123”)
net.Start(“passdata123”)
net.WriteInt(TAmount,8)
net.WriteInt(DAmount,8)
net.Send(ply)
else
pass = {
TAmount = 0,
DAmount = 0,
steamid = ply:SteamID()
}
local userDataFile = “pass/”…ply:UniqueID()…".txt"
file.Write(userDataFile, util.TableToJSON(pass))
util.AddNetworkString(“passdata123”)
net.Start(“passdata123”)
net.WriteInt(pass.TAmount,8)
net.WriteInt(pass.DAmount,8)
net.Send(ply)
end
end
hook.Add(“PlayerInitialSpawn”,“LoadPlayerData”,LoadData)

function SaveData(ply)
net.Receive(“savedata123”, function(len,ply)
local TAmount = net.ReadInt(8)
local DAmount = net.ReadInt(8)
end)
local userDataFile = “pass/”…ply:UniqueID()…".txt"
local pass = {
TAmount = TAmount,
DAmount = TAmount,
steamid = ply:SteamID()
}
file.Write(userDataFile, util.TableToJSON(pass))
end
hook.Add(“PlayerDisconnected”,“SavePlayerData”, SaveData)
hook.Add(“ShutDown”,“SavePlayerData2”,SaveData)
[/LUA]

That’s what I have after I looked at the examples on the wiki. Still printing an error.
Sorry for beeing a noob. :c

I says on the wiki not to use net.Receive in a function…

PlayerInitialSpawn is serversided, not clientsided

Server
[LUA]
function Initialize()
if not file.IsDir( “pass”, “DATA” ) then
file.CreateDir(“pass”, “DATA” )
end
end
hook.Add(“Initialize”,“InitializePass”, Initialize)

function LoadData(ply)
local userDataFile = “pass/”…ply:UniqueID()…".txt"
if file.Exists(userDataFile, “DATA”) then
local playerData = util.JSONToTable(file.Read(userDataFile, “DATA”) )
local TAmount = playerData.TAmount
local DAmount = playerData.DAmount
util.AddNetworkString(“passdata123”)
net.Start(“passdata123”)
net.WriteInt(TAmount,8)
net.WriteInt(DAmount,8)
net.Send(ply)
else
pass = {
TAmount = 0,
DAmount = 0,
steamid = ply:SteamID()
}
local userDataFile = “pass/”…ply:UniqueID()…".txt"
file.Write(userDataFile, util.TableToJSON(pass))
util.AddNetworkString(“passdata123”)
net.Start(“passdata123”)
net.WriteInt(pass.TAmount,8)
net.WriteInt(pass.DAmount,8)
net.Send(ply)
end
end
hook.Add(“PlayerInitialSpawn”,“LoadPlayerData”,LoadData)

net.Receive(“savedata123”, function(len,ply)
local TAmount = net.ReadInt(8)
local DAmount = net.ReadInt(8)
local userDataFile = “pass/”…ply:UniqueID()…".txt"
local pass = {
TAmount = TAmount,
DAmount = TAmount,
steamid = ply:SteamID()
}
file.Write(userDataFile, util.TableToJSON(pass))
end)
[/LUA]

CLIENT
[LUA]
net.Receive(“passdata123”, function(len,ply)
TAmount = net.ReadInt(8)
DAmount = net.ReadInt(8)
end

// Bunch of codes

function sendBackData(ply)
util.AddNetworkString(“savedata123”)
net.Start(“savedata123”)
net.WriteInt(TAmount,8)
net.WriteInt(DAmount,8)
net.SendToServer()
end
hook.Add(“PlayerDisconnected”,“sendBackData”,sendBackData)
hook.Add(“ShutDown”,“sendBackData2”,sendBackData)
[/LUA]

GM:PlayerDisconnected is a server side hook.

util.AddNetworkString has to be called a wee bit before you attempt to send a message using it, e.g. as soon as the file loads.

I don’t know if a net message sent inside

GM:ShutDown would actually be sent to the server, I’d test that the server is even receiving this.

how can I send the values to the server if the player leaves?
Im a stupid new lua coder.

Sorry about that, meant to say I didn’t know if a net message sent inside of

GM:ShutDown would be sent to the server.

Are these values meant to be manipulated by the clients? If not, you don’t need to send the value at all. If yes, send the new values once they’ve changed and store them server side.

You should never need to send the values to the server. Never rely on the client for something like that. Extremely exploitable.

Only make changes to that value on the server, not the client.

But how could I code it, because you open the menu with !pass, and then you click on use.

Should I send a message to the server and manipulate it there and send the value back so he knows how many tries he has left?

[editline]27th January 2017[/editline]

I still get the same error :c

Server
[LUA]
util.AddNetworkString(“passdata123T”)
util.AddNetworkString(“passdata123D”)

function Initialize()
if not file.IsDir( “pass”, “DATA” ) then
file.CreateDir(“pass”, “DATA” )
end
end
hook.Add(“Initialize”,“InitializePass”, Initialize)

function LoadData(ply)
local userDataFile = “pass/”…ply:UniqueID()…".txt"
if file.Exists(userDataFile, “DATA”) then
local playerData = util.JSONToTable(file.Read(userDataFile, “DATA”) )
local TAmount = playerData.TAmount
local DAmount = playerData.DAmount
net.Start(“passdata123T”)
net.WriteInt(TAmount,32)
net.Send(ply)
net.Start(“passdata123D”)
net.WriteInt(DAmount,32)
net.Send(ply)
else
pass = {
TAmount = 0,
DAmount = 0,
steamid = ply:SteamID()
}
local userDataFile = “pass/”…ply:UniqueID()…".txt"
file.Write(userDataFile, util.TableToJSON(pass))
net.Start(“passdata123T”)
net.WriteInt(pass.TAmount,32)
net.Send(ply)
net.Start(“passdata123D”)
net.WriteInt(pass.DAmount,32)
net.Send(ply)
end
end
hook.Add(“PlayerInitialSpawn”,“LoadPlayerData”,LoadData)

net.Receive(“sendMinusValueT”, function(len,ply)
if(net.ReadString(4) == “yes”) then
local TAmount = 0
net.Start(“passdata123T”)
net.WriteInt(TAmount,32)
net.Send(ply)
else
local TAmount = TAmount - net.ReadInt(32)
net.Start(“passdata123T”)
net.WriteInt(TAmount,32)
net.Send(ply)
end
end)

net.Receive(“sendMinusValueD”, function(len,ply)
if(net.ReadString(4) == “yes”) then
local DAmount = 0
net.Start(“passdata123D”)
net.WriteInt(DAmount,32)
net.Send(ply)
else
local DAmount = DAmount - net.ReadInt(32)
net.Start(“passdata123D”)
net.WriteInt(TAmount,32)
net.Send(ply)
end
end)

net.Receive(“sendPlusValueT”, function(len,ply)
local TAmount = TAmount + net.ReadInt(32)
net.Start(“passdata123T”)
net.WriteInt(TAmount,32)
net.Send(ply)
end)

net.Receive(“sendPlusValueD”, function(len,ply)
local DAmount = DAmount + net.ReadInt(32)
net.Start(“passdata123D”)
net.WriteInt(DAmount,32)
net.Send(ply)
end)
[/LUA]

Client
[LUA]
util.AddNetworkString(“sendMinusValueT”)
util.AddNetworkString(“sendMinusValueD”)
util.AddNetworkString(“sendPlusValueT”)
util.AddNetworkString(“sendPlusValueD”)

hook.Add(“TTTPrepareRound”,“Prepare”,function()
prepare = false
end)

net.Receive(“passdata123T”, function(len,ply)
TAmount = net.ReadInt(32)
end)

net.Receive(“passdata123D”, function(len,ply)
DAmount = net.ReadInt(32)
end)

function Frame(ply, text, public)
if (string.lower(text) == “!pass” ) then
local Frame = vgui.Create( “DFrame” )
Frame:SetPos(surface.ScreenHeight() - 300 ,surface.ScreenWidth() - 1250)
Frame:SetSize( 500, 500 )
Frame:SetTitle( “TTT-Passes” )
Frame:SetVisible( true )
Frame:SetDraggable( true )
Frame:ShowCloseButton( true )
Frame:MakePopup()
Frame.Paint = function( self, w, h )
draw.RoundedBox( 0, 0, 0, w, h, Color( 150, 150, 150,200 ) )
end

	local FrameView = vgui.Create( "DListView", Frame )
	FrameView:SetPos(50,70)
	FrameView:SetSize(400,400)
	FrameView:SetMultiSelect(false)
	FrameView:AddColumn("T-Amount")
	FrameView:AddColumn("D-Amount")
	FrameView.Paint = function( self, w, h )
		draw.RoundedBox( 0, 0, 0, w, h, Color(100,100,100,200))
	end
	FrameView:AddLine(TAmount, DAmount)
	FrameView.OnRowRightClick = function( panel, rowId, row )
		local menu = DermaMenu()
		menu:AddOption( "Use Traitor-pass", function()
			if(TAmount > 0) then
				if(prepare == false) then
					net.Start("sendMinusValueT")
						net.WriteString("no",4)
						net.WriteInt(1,32)
					net.SendToServer()
					Frame:Close()
					prepare = true
					ply:ChatPrint("You just used a Traitor pass!")
				else
					ply:ChatPrint("You can't use this at the moment!")
				end
			else
				ply:ChatPrint("You don't have enough T-passes!")
			end
		end )
		menu:AddOption( "Use Detective-pass", function()
			if(DAmount > 0) then
				if(prepare == false) then
					net.Start("sendMinusValueD")
						net.WriteString("no",4)
						net.WriteInt(1,32)
					net.SendToServer()
					Frame:Close()
					prepare = true
					ply:ChatPrint("You just used a Detective pass!")
				else
					ply:ChatPrint("You can't use this at the moment!")
				end
			else
				ply:ChatPrint("You don't have enough D-Passes!")
			end
		end )
		menu:Open()
	end
end

end
hook.Add(“OnPlayerChat”, “Chat_Command”, Frame)

function AdminFrame(ply, text, public)
if (string.lower(text) == “!adminpass” ) then
if(ply:IsUserGroup(“owner”)) then
local Frame2 = vgui.Create( “DFrame” )
Frame2:SetPos(surface.ScreenHeight() - 300 ,surface.ScreenWidth() - 1250)
Frame2:SetSize( 500, 500 )
Frame2:SetTitle( “TTT-Passes [ADMIN]” )
Frame2:SetVisible( true )
Frame2:SetDraggable( true )
Frame2:ShowCloseButton( true )
Frame2:MakePopup()
Frame2.Paint = function( self, w, h )
draw.RoundedBox( 0, 0, 0, w, h, Color( 150, 150, 150,200 ) )
end

		local FrameView2 = vgui.Create( "DListView", Frame2 )
		FrameView2:SetPos(50,70)
		FrameView2:SetSize(400,400)
		FrameView2:SetMultiSelect(false)
		FrameView2:AddColumn("Name")
		FrameView2:AddColumn("T-Amount")
		FrameView2:AddColumn("D-Amount")
		FrameView2.Paint = function( self, w, h )
			draw.RoundedBox( 0, 0, 0, w, h, Color(100,100,100,200))
		end
		for k,v in pairs(player.GetAll()) do
			FrameView2:AddLine(v:Nick(), TAmount, DAmount)
		end
		FrameView2.OnRowRightClick = function( panel, rowId, row )
			local playerName = row:GetColumnText( 1 )
			local menu = DermaMenu()
			menu:AddOption( "Get SteamID", function()
				SetClipboardText(ply:SteamID()) 
			end )
			menu:AddOption( "Add T-Pass", function()
				local FrameText = vgui.Create( "DTextEntry",FrameView2)
				FrameText:Center()
				FrameText:SetTall(20)
				FrameText:SetWide(50)
				FrameText:RequestFocus() 
				FrameText:SetEnterAllowed(true)
				FrameText:SetFocusTopLevel(true)
				FrameText.OnEnter = function()
					net.Start("sendPlusValueT")
						net.WriteInt(FrameText:GetValue(),32)
					net.SendToServer()
					FrameText:SetVisible(false)
					FrameView2:Clear()
					for k,v in pairs(player.GetAll()) do
						FrameView2:AddLine(v:Nick(), TAmount, DAmount)
					end
				end
			end )
			menu:AddOption( "Add D-Pass", function()
				local FrameText = vgui.Create( "DTextEntry",FrameView2)
				FrameText:Center()
				FrameText:SetTall(20)
				FrameText:SetWide(50)
				FrameText:RequestFocus() 
				FrameText:SetEnterAllowed(true)
				FrameText:SetFocusTopLevel(true)
				FrameText.OnEnter = function()
					net.Start("sendPlusValueD")
						net.WriteInt(FrameText:GetValue(),32)
					net.SendToServer()
					FrameText:SetVisible(false)
					FrameView2:Clear()
					for k,v in pairs(player.GetAll()) do
						FrameView2:AddLine(v:Nick(), TAmount, DAmount)
					end
				end
			end )
			menu:AddOption( "Subtract T-Pass", function()
				local FrameText = vgui.Create( "DTextEntry",FrameView2)
				FrameText:Center()
				FrameText:SetTall(20)
				FrameText:SetWide(50)
				FrameText:SetEnterAllowed(true)
				FrameText:RequestFocus() 
				FrameText:SetFocusTopLevel(true)
				FrameText.OnEnter = function()
					FrameText:SetVisible(false)
					if(FrameText:GetValue() > TAmount) then
						net.Start("sendMinusValueT")
							net.WriteString("yes",4)
						net.SendToServer()
					else
						net.Start("sendMinusValueT")
							net.WriteString("no",4)
							net.WriteInt(FrameText:GetValue(),32)
						net.SendToServer()
					end
					FrameView2:Clear()
					for k,v in pairs(player.GetAll()) do
						FrameView2:AddLine(v:Nick(), TAmount, DAmount)
					end
				end
			end )
			menu:AddOption( "Subtract D-Pass", function()
				local FrameText = vgui.Create( "DTextEntry",FrameView2)
				FrameText:Center()
				FrameText:SetTall(20)
				FrameText:SetWide(50)
				FrameText:RequestFocus() 
				FrameText:SetEnterAllowed(true)
				FrameText:SetFocusTopLevel(true)
				FrameText.OnEnter = function()
					if(FrameText:GetValue() > DAmount) then
						net.Start("sendMinusValueD")
							net.WriteString("yes",4)
						net.SendToServer()
					else
						net.Start("sendMinusValueD")
							net.WriteString("no",4)
							net.WriteInt(FrameText:GetValue(),32)
						net.SendToServer()
					end
					FrameView2:Clear()
					for k,v in pairs(player.GetAll()) do
						FrameView2:AddLine(v:Nick(), TAmount, DAmount)
					end
				end
			end )
			menu:Open()
		end
	else
		ply:ChatPrint( "You have no rights do open this panel!" ) 
	end
end

end
hook.Add(“OnPlayerChat”, “Chat_Command2”, AdminFrame)
[/LUA]

[editline]27th January 2017[/editline]

A bit long, I know but maybe the net.SendToServer() are important.

[editline]27th January 2017[/editline]

I can shorten it if you don’T want to read all of that code.

Just tell me

It’d be helpful if you could post the error that you’re getting.

One note though,



local TAmount = TAmount + net.ReadInt(32)

-- or

local DAmount = DAmount + net.ReadInt(32)


Will error unless (T/D)Amount is already a local variable available in the scope, or a global variable.

[editline]27th January 2017[/editline]

Just saw the note about it being the same error, and to answer that I’d still need to know which line it happened on. If it happened on the first one, it’s an issue of that table not having the value, and it’s impossible for it to happen on the second one.

I’m getting connection errors, all the addons that have a network based system are printing the same error, nil value something. But here’s the code.
[LUA]

[ERROR] addons/pointshop/lua/pointshop/sv_player_extension.lua:287: attempt to index field ‘PS_Items’ (a nil value)

  1. PS_HasItem - addons/pointshop/lua/pointshop/sv_player_extension.lua:287

  2. PS_HasItemEquipped - addons/pointshop/lua/pointshop/sv_player_extension.lua:291

3. fn - addons/pointshop/lua/pointshop/sh_init.lua:145

 4. unknown - addons/ulib/lua/ulib/shared/hook.lua:109

[ERROR] addons/pointshop/lua/pointshop/sv_player_extension.lua:287: attempt to index field ‘PS_Items’ (a nil value)

  1. PS_HasItem - addons/pointshop/lua/pointshop/sv_player_extension.lua:287

  2. PS_HasItemEquipped - addons/pointshop/lua/pointshop/sv_player_extension.lua:291

3. fn - addons/pointshop/lua/pointshop/sh_init.lua:145

 4. unknown - addons/ulib/lua/ulib/shared/hook.lua:109

[ERROR] addons/pointshop/lua/pointshop/sv_player_extension.lua:287: attempt to index field ‘PS_Items’ (a nil value)

  1. PS_HasItem - addons/pointshop/lua/pointshop/sv_player_extension.lua:287

  2. PS_HasItemEquipped - addons/pointshop/lua/pointshop/sv_player_extension.lua:291

3. fn - addons/pointshop/lua/pointshop/sh_init.lua:145

 4. unknown - addons/ulib/lua/ulib/shared/hook.lua:109

[/LUA]

[editline]28th January 2017[/editline]

Well, basicly I don’t get any other message than the message above. I have no idea which net.libary is wrong or if something else in the code is stupidly wrong.

Hmmmh… Btw the (T/D)Amount is already a global variable.

Is there any specific reason why you’re using a file instead of sql/pdata?

I heard that sql is bad, and someone recommended me to use a file.