[How to] JSON with FiveM

MrtviNiz = {}
ucitaj = LoadResourceFile("dodatci", "sacuvanemaske.json")
MrtviNiz = json.decode(ucitaj)

RegisterServerEvent('maske:json')
AddEventHandler('maske:json', function(skin, accessory)
	MrtviNiz = {}
	local _source = source
	local xPlayer = ESX.GetPlayerFromId(_source)
	
	
	local itemSkin = {}
	local item1 = string.lower(accessory) .. '_1'
	local item2 = string.lower(accessory) .. '_2'
	itemSkin[item1] = skin[item1]
	itemSkin[item2] = skin[item2]

	local maskaa = ({Vlasnik = xPlayer.identifier, Maska1 = itemSkin[item1], Maska2 = itemSkin[item2]})

	table.insert(MrtviNiz, maskaa)

	SaveResourceFile("dodatci", "sacuvanemaske.json", json.encode(MrtviNiz), -1)
end)

acutally, it works now, sorry haha. Now do you maybe know how I can get that data, for exaple when player joins server scripts checks does he have it, basiclly how I can get some data from json

1 Like

pull the information off the table

1 Like

yea i know i need to do that, but how xd?

RegisterServerEvent('maske:json')
AddEventHandler('maske:json', function(skin, accessory)
	local _source = source
	local xPlayer = ESX.GetPlayerFromId(_source)
	if xPlayer then
		local identifier = xPlayer.identifier
		
		local itemSkin = {}
		local item1 = string.lower(accessory) .. '_1'
		local item2 = string.lower(accessory) .. '_2'
		itemSkin[item1] = skin[item1]
		itemSkin[item2] = skin[item2]

		local maskaa = ({Vlasnik = identifier, Maska1 = itemSkin[item1], Maska2 = itemSkin[item2]})
		local playerFound = false

		--checks if player is already in the table, if so, it will replace old values with new ones
		for k,v in pairs(MrtviNiz) do
			if v.Vlasnik == identifier then
				v.Maska1 = itemSkin[item1]
				v.Maska2 =	itemSkin[item2]
				playerFound = true
				break
			end
		end
		
		--if player was not found in the table it will create new entry
		if playerFound == false then
			table.insert(MrtviNiz, maskaa)
		end
		
		SaveResourceFile("dodatci", "sacuvanemaske.json", json.encode(MrtviNiz), -1)
	end
end)

RegisterServerEvent('playerJoined')
AddEventHandler('playerJoined', function()
	--make sure file is loaded before this event, you only need to load it once
	local _source = source
	local xPlayer = ESX.GetPlayerFromId(_source)
	if xPlayer then
		local identifier = xPlayer.identifier

		local mask1, mask2
		local playerFound = false

		for k,v in pairs(MrtviNiz) do
			if v.Vlasnik == identifier then
				mask1 = v.Maska1
				mask2 = v.Maska2
				playerFound = true
				break
			end
		end

		--if player was found in the table
		if playerFound then
			--trigger whatever you need to do with the skins
		end
	end
end)
2 Likes

thanks alot

should this need to be triggered somewhere in client ?

RegisterServerEvent('playerJoined')
AddEventHandler('playerJoined', function()
	--make sure file is loaded before this event, you only need to load it once
	local _source = source
	local xPlayer = ESX.GetPlayerFromId(_source)
	if xPlayer then
		local identifier = xPlayer.identifier

		local mask1, mask2
		local playerFound = false

		for k,v in pairs(MrtviNiz) do
			if v.Vlasnik == identifier then
				mask1 = v.Maska1
				mask2 = v.Maska2
				playerFound = true
				break
			end
		end

		--if player was found in the table
		if playerFound then
			--trigger whatever you need to do with the skins
		end
	end
end)


Can someone help was told to edit config.json file enter database details in config.json I’m so lost new to all this

JSON is very fast meta-data base

Thanks, this’ll help with QuikkAdmin :slight_smile:

Is there any advantage of using json if your code is just in lua anyways?

really late reply, but since the JSON is just a temp database you can use it to store it temporarily, instead of using a database where everything is stored, even on reboot.

Hello, I’m not a big expert on the dev side, but I’m not doing so badly. not long ago I was helping a friend to set up his server, however he was using php more for sql input, are these precisely with a json?