Help with using file.Read/file.Write to save/load a string

Hello, I am new to lua and attempting to learn the file.Read/file.Write saving method. I have encountered a problem where is seems as if the string isn’t being saved. Any ideas?



if CLIENT then
	net.Receive( "openwordmenu", function()
		local fr = vgui.Create( "DFrame" )
		fr:SetSize( 300, 80 )
		fr:SetTitle( "Choose your word" )
		fr:Center()
		fr:MakePopup()
		function fr:Paint( w, h )
			draw.RoundedBox( 0, 0, 0, w, h, Color( 0, 0, 0, 200 ) )
			draw.RoundedBox( 0, 0, 0, w, 25, Color( 0, 0, 0, 100 ) )
		end
		local lbl = vgui.Create( "DLabel", fr )
		lbl:SetText( "Choose your word below:" )
		lbl:SetPos( 95, 30 )
		lbl:SizeToContents()
		local entry = vgui.Create( "DTextEntry", fr )
		entry:SetPos( 5, 50 )
		entry:SetSize( fr:GetWide() - 10, 20 )
		entry:SetText( "" )
		entry.OnEnter = function( self )
			fr:SetVisible( false )
			net.Start( "sendword" )
			net.WriteString( self:GetValue() )
			net.SendToServer()
		end
	end )
end

if SERVER then
	PlayerWord = {}
	
	local function saveData()
		file.Write("PlayerWord.txt", util.TableToJSON(PlayerWord))
	end

	local function loadData()
		if not file.Exists("PlayerWord.txt", "DATA") then saveData() end
		PlayerWord = util.JSONToTable(file.Read("PlayerWord.txt", "DATA"))
	end
	
	util.AddNetworkString( "sendword" )
	net.Receive( "sendword", function()
		local werd = net.ReadString()
		PlayerWord[ply:SteamID()] = werd
		saveData()
	end )

	hook.Add("PlayerInitialSpawn", "Message", function(ply)
		loadData()
		if PlayerWord[ply:SteamID()] then
			ply:ChatPrint( "Your word is "..PlayerWord[ply:SteamID()] )
		else
			ply:ChatPrint( "You haven't set-up your word yet! Press f1 to choose your word." )
		end
	end)
	
	util.AddNetworkString( "openwordmenu" )
	hook.Add("ShowHelp", "RandomNumber", function(ply)
		net.Start( "openwordmenu" )
		net.Send( ply )
	end)
end


You didn’t define ply inside the sendword net.Receive function - it’s probably erroring in there.

Derp, wow i’m stupid.

Edit:

It seems as though problem is still occurring.

net.Receive has two return arguments.


net.Receive("messagename",function(messageLength,ply)
-- code
end)

Will work.
messageLength is just the length of the usermessage. It’s not very important and it confuses me to why it was set as the first argument while being implemented.

Thanks, it works now! Fixed.