I know about the existence of mySQL but I’m writing all of my resources on my own and I prefer to save player progress and data on file.
The objective is to create file Playername_Datatype.txt for each player as well as for each data type (for example money, last position, unlocked cars, …)
At the moment I’m testing the resource just to save, and read money (GetMoney, AddMoney, SetMoney are the functions)
With this resources I need to be able to call these functions like GetMoney from other resources (serverside).
This is what I’ve done.
CLIENTSIDE
Citizen.CreateThread(function()
while true do
Citizen.Wait(1)
if GetEntityHealth(GetPlayerPed(-1)) <= 0 then
coords = GetEntityCoords(GetPlayerPed(-1))
TriggerServerEvent('SaveData', 'lastpos', coords)
Citizen.Wait(10000)
end
end
end)
SERVERSIDE
dir = "database/"
function GetMoney(player)
if player == nil then
player = -1
end
TriggerServerEvent('ReadData', 'money', player)
end
function AddMoney(player, amount)
if amount ~= nil then
if player == nil then
player = -1
end
toadd = tonumber(GetMoney(player)) + tonumber(amount)
TriggerServerEvent('SaveData', player, 'money', toadd)
end
end
function SetMoney(player, amount)
if amount ~= nil then
if player == nil then
player = -1
end
toset = tonumber(amount)
TriggerServerEvent('SaveData', player, 'money', toset)
end
end
function RemoveMoney(player, amount)
if amount ~= nil then
if player == nil then
player = -1
end
GetMoney(player)
toremove = tonumber(readdata) - tonumber(amount)
TriggerServerEvent('SaveData', player, 'money', toremove)
end
end
RegisterServerEvent('SaveData')
AddEventHandler('SaveData', function(player, datatype, data)
if player == nil then
player = -1
end
if datatype == 'money' then
nametowrite = (GetPlayerName(player).. '_money.txt')
elseif datatype ~= unlocks then
nametowrite = (GetPlayerName(player).. '_' ..datatype.. '.txt')
end
if not DoesFileExist(nametowrite) then
CreateFile(nametowrite)
end
print(datatype.. ' - SAVED - ' ..data)
end)
RegisterServerEvent('ReadData')
AddEventHandler('ReadData', function(datatype, player)
if player == nil then
player = -1
end
if datatype ~= unlocks then
nametoread = (GetPlayerName(player).. '_' ..datatype.. '.txt')
print(datatype.. ' - READ - ' ..data)
end
ReadFile(nametoread)
end)
---------------------------------------------------------------------------
function DoesFileExist(name)
local dir = dir .. name
local file = io.open(dir, "r")
if (file ~= nil) then
io.close( file )
return true
else
return false
end
end
function CreateFile( name )
local dir = dir .. name
local file = io.open(dir,'w')
file:write(data.. "\n")
file:close()
end
function ReadFile(name)
local dir = dir .. name
local file = io.open(dir,'r')
readdata = file:read('*all')
end
The issue with this is that the resource stops loading right after player hit connect on FiveM client. (This prevents players to join the server). (The console doesn’t give any error while starting the resource)
I can’t find where the error is also because it’s my first time playing with io.open, file:read, … .
Resource: database.rar (1.2 KB)