Saving files, explanation?

Hi,
so I’m new in lua and I don’t get the saving stuff because there is no quiet good tutorial on the internet, atleast I didn’t find one!

So I watched a youtube tutorial and I have this stuff. What is wrong?

CLIENTSIDE
[LUA]
TAmount = LocalPlayer():GetNWInt(“pass”)
DAmount = LocalPlayer():GetNWInt(“pass”)
[/Lua]

SERVER SIDE
[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”) )
file.Write(userDataFile, util.TableToJSON(playerData))
else
pass = {
TAmount = 0,
DAmount = 0,
steamid = ply:SteamID()
}
file.Write(userDataFile, util.TableToJSON(pass))
end
end
hook.Add(“PlayerInitialSpawn”,“LoadPlayerData”,LoadData)

function SaveData(ply)
local userDataFile = “pass/”…ply:UniqueID()…".txt"
ply:SetNWInt(“TAmount”, TAmount)
ply:SetNWInt(“DAmount”, DAmount)
end
hook.Add(“PlayerDisconnected”,“SavePlayerData”, SaveData)
[/LUA]

I don’t get any error message, but neither do the files save. When I join my saved files stay the same.

All bests
Dagun

You are setting into TAmount and DAmount, yet you are retrieving pass twice. Looks like a typo.

How can I retrive it once?
Because I have no clue how to connect TAmount with the written data.

LocalPlayer():GetNWInt(“TAmount”)

I’m trying right now.

[editline]26th January 2017[/editline]

It doesn’t work.
I tried
[LUA]
TAmount = LocalPlayer():GetNWInt(“TAmount”)
// and
LocalPlayer():GetNWInt(“TAmount”)
[/LUA]
But it doesn’t save. My save function is most likly false.

[editline]26th January 2017[/editline]

It doesn’t work.
I tried
[LUA]
TAmount = LocalPlayer():GetNWInt(“TAmount”)
// and
LocalPlayer():GetNWInt(“TAmount”)
[/LUA]
But it doesn’t save. My save function is most likly false.

[editline]26th January 2017[/editline]

Im trying something out but with failure. The code is above on my first post.

Feel free to be nice and give me an idea of how I could / should do it.
:slight_smile:

Can you post more code context? Also, are you getting it after you set it?

Server side script
[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”) )
file.Write(userDataFile, util.TableToJSON(playerData))
else
pass = {
TAmount = 0,
DAmount = 0,
steamid = ply:SteamID()
}
file.Write(userDataFile, util.TableToJSON(pass))
end
end
hook.Add(“PlayerInitialSpawn”,“LoadPlayerData”,LoadData)

function SaveData(ply)
local userDataFile = “pass/”…ply:UniqueID()…".txt"
local playerData = util.JSONToTable(file.Read(userDataFile, “DATA”) )
ply:SetNWInt(userDataFile, util.TableToJSON(playerData))
end
hook.Add(“PlayerDisconnected”,“SavePlayerData”, SaveData)
[/LUA]

CLIENT SIDE
[LUA]
TAmount = LocalPlayer():GetNWInt(“TAmount”)
DAmount = LocalPlayer():GetNWInt(“DAmount”)
// Underneath this code there are bunch of codes changing the value of above
[/LUA]

You just reposted the code from earlier; can you give more clientside context?

[editline]26th January 2017[/editline]

Also, you broke SaveData since you’re setting the NWInt to a filepath, not TAmount or DAmount.

[LUA]
TAmount = LocalPlayer():GetNWInt(“TAmount”)
DAmount = LocalPlayer():GetNWInt(“DAmount”)

hook.Add(“TTTPrepareRound”,“Prepare”,function()
prepare = false
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
					TAmount = TAmount - 1
					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
					DAmount = DAmount - 1
					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()
					TAmount = TAmount + FrameText:GetValue()
					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()
					DAmount = DAmount + FrameText:GetValue()
					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()
					TAmount = TAmount - FrameText:GetValue()
					FrameText:SetVisible(false)
					if(TAmount < 0) then
						TAmount = TAmount - TAmount
					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()
					DAmount = DAmount - FrameText:GetValue()
					FrameText:SetVisible(false)
					if(DAmount < 0) then
						DAmount = DAmount - DAmount
					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]

Don’t store TAmount and DAmount outside of the function; call it every time so if it updates, the client sees the updated value.

Now I tried something out and I read the wiki more than once but I have this code and it still prints me that
[LUA] Error: Trying to network (receive) unacceptable type (table) [/LUA]
Even though I sended only 1 value?

Server side
[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
ply:SetNWInt(“TAmount”,TAmount)
ply:SetNWInt(“DAmount”,DAmount)
else
pass = {
TAmount = 0,
DAmount = 0,
steamid = ply:SteamID()
}
file.Write(userDataFile, util.TableToJSON(pass))
end
end
hook.Add(“PlayerInitialSpawn”,“LoadPlayerData”,LoadData)

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

Client side
[LUA]
TAmount = LocalPlayer():GetNWInt(“TAmount”)
DAmount = LocalPlayer():GetNWInt(“DAmount”)
// Bunch of code, same as above
function sendBackData(ply)
ply:SetNWInt(“TAmount”,TAmount)
ply:SetNWInt(“DAmount”,DAmount)
end
hook.Add(“PlayerDisconnected”,“sendBackData”,sendBackData)
[/LUA]

[editline]26th January 2017[/editline]

I loaded the file and converted it into a lua table.
It’s called playerData and I took one value -> playerData.TAmount

I sended that value to the client but it tells me that I can’t send a table, but I am not?

Im thinking to much in C, maybe.
But I can’t find the error myself.

[editline]26th January 2017[/editline]

I haven’t found any solution yet.

Help please :C

You could also just you PData instead of writing it as a file, and you need to take into consideration that on a mapchange the players dont get disconnected, you need to hook something to GM:ShutDown

Edit: And you cant set a SetNW* Clientside, it will only set it on the client and not on a server, if you want to send something to the server you need to use concommands or the net library