TableToJSON file.Write & file.Read

So, I have been trying and trying to get this code to work but, can’t on linux any suggestions?



local function SaveVehicles()
	local str = util.TableToJSON(NPCSHOP.PlayerVehicles)
	file.Write( "npcshopsaves.txt", str )
end
local function LoadVehicles()
	local str = file.Read( "npcshopsaves.txt", "DATA" ) or "[]"
	NPCSHOP.PlayerVehicles = util.JSONToTable(str)
end


What is the problem/error?



local BuddyJSON = file.Read("kkb_buddies.txt", "DATA")
if ( BuddyJSON != nil ) then
	KKB.BuddyList = util.JSONToTable( BuddyJSON )
end

This code works on linux, i’ve used it before.

It’s this little piece your having trouble with.


 or "[]"

An empty table would be represented with


{}

You might want to try this


local function SaveVehicles()
	local str = util.TableToJSON(NPCSHOP.PlayerVehicles)
	file.Write( "npcshopsaves.txt", str )
end
local function LoadVehicles()
	local str = file.Read( "npcshopsaves.txt", "DATA" )
	if ( str != nil ) then
		NPCSHOP.PlayerVehicles = util.JSONToTable(str)
	else
		NPCSHOP.PlayerVehicles = {}
	end
end

That’s the same thing, the or “” is just a fallback given the file is empty.

Your right I didn’t even think about the nil on creation. Thank you so much been up for far to long.

Willox, I would really like to know why you disagree with my previous post, I think I covered it thoroughly…?

Your solution was correct, but


local str = file.Read( "npcshopsaves.txt", "DATA" ) or "[]"

should work correctly as “” is an empty array in JSON

Thank you, I did not know that :slight_smile: