Hello, I am having some problems with this script I’m making for my server.
I’ll first post the resource and then the error with the explanation. Somehow I can’t get around it!
CLIENT SIDE
Citizen.CreateThread(function()
while true do
Wait(1)
if IsPedDeadOrDying(GetPlayerPed(-1)) then
Wait(2000)
coords = GetEntityCoords(GetPlayerPed(-1))
if not IsEntityInWater(GetPlayerPed(-1)) then
Wait(1000)
SetEntityCoords(GetPlayerPed(-1), coords, 0, 0, 0, 1)
end
SaveCoords(table.unpack(coords))
end
if IsPlayerLoggingInNp() then
Wait(10000)
TriggerServerEvent('ReadSavegame', source)
SetEntityCoords(GetPlayerPed(-1), tonumber(coordsx), tonumber(coordsy), tonumber(coordsz), 0, 0, 0, 1)
end
end
end)
function SaveCoords(x,y,z)
TriggerServerEvent('WriteSavegame', source, 1, x)
TriggerServerEvent('WriteSavegame', source, 2, y)
TriggerServerEvent('WriteSavegame', source, 3, z)
end
SERVER SIDE
databasedir = 'database'
RegisterServerEvent('ReadSavegame')
RegisterServerEvent('WriteSavegame')
AddEventHandler('ReadSavegame', function(source)
if GetIdFromSource("steam", source) ~= nil then
id = GetIdFromSource("steam", source)
else
id = 'common'
end
savename = databasedir.. '/' ..id.. '.txt'
local data = LoadSavegame(savename)
stringsplit(data, ',')
coordsx = data[1]
coordsy = data[2]
coordsz = data[3]
end)
AddEventHandler('WriteSavegame', function(source, datatype, writeme)
if GetIdFromSource("steam", source) ~= nil then
id = GetIdFromSource("steam", source)
else
id = 'common'
end
savename = databasedir.. '/' ..id.. '.txt'
local data = LoadSavegame(savename)
stringsplit(data, ',')
towrite[1] = data[1] -- X
towrite[2] = data[2] -- Y
towrite[3] = data[3] -- Z
--ADD HERE FURTHER DATATYPES
towrite[tonumber(datatype)] = writeme
UploadSavegame(savename, towrite[1].. ',' ..towrite[2].. ',' ..towrite[3])
end)
-- FUNCTIONS
function LoadSavegame(path)
if fileExists(path) then
local file = io.open(path, 'w+')
local buffer = file:read("*a")
file:close()
return buffer
else
return false
end
end
function UploadSavegame(path, data)
local file = io.open(path, 'w+')
file:write(tostring(data))
file:flush()
file:close()
end
function fileExists(path)
local file = io.open(path, "rb")
if file then
file:close()
end
return file ~= nil
end
function stringsplit(inputstr, sep)
if sep == nil then
sep = "%s"
end
local t={} ; i=1
for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
t[i] = str
i = i + 1
end
return t
end
function GetIdFromSource(Type, ID)
local IDs = GetPlayerIdentifiers(ID)
for k, CurrentID in pairs(IDs) do
local ID = stringsplit(CurrentID, ":")
if (ID[1] == string.lower(Type)) then
return ID[2]
end
end
return nil
end
ERROR ON THE CONSOLE
cfx> InvokeNative: execution failed: Argument at index 0 was null.
Error running system event handling function for resource database: citizen:/scripting/lua/scheduler.lua:41: Failed to execute thread: Execution of native 00000000ff7f66ab in script host failed.
stack traceback:
[C]: in upvalue '_in'
citizen:/scripting/lua/natives_server.lua:154: in function 'GetNumPlayerIdentifiers'
citizen:/scripting/lua/scheduler.lua:294: in function 'GetPlayerIdentifiers'
@database/sv_database.lua:86: in function 'GetIdFromSource'
@database/sv_database.lua:24: in upvalue 'handler'
citizen:/scripting/lua/scheduler.lua:219: in function <citizen:/scripting/lua/scheduler.lua:218>
stack traceback:
[C]: in function 'error'
citizen:/scripting/lua/scheduler.lua:41: in field 'CreateThreadNow'
citizen:/scripting/lua/scheduler.lua:218: in function <citizen:/scripting/lua/scheduler.lua:182>
This happens every time I die ingame, when the resource is supposed to write the savegame but obviously doesn’t!
Please explain me this the easy way if you could, I’m still learning the basics of lua