• Unfortunately, we have experienced significant hard drive damage that requires urgent maintenance and rebuilding. The forum will be a state of read only until we install our new drives and rebuild all the configurations needed. Please follow our Facebook page for updates, we will be back up shortly! (The forum could go offline at any given time due to the nature of the failed drives whilst awaiting the upgrades.) When you see an Incapsula error, you know we are in the process of migration.

Problem with LUA scripts and DB

Junior Spellweaver
Joined
Nov 23, 2008
Messages
146
Reaction score
11
Hello guys.

Here is my problem. I've trying to write a script to save players coordinates (from GTA V) directly into the database. So then when we relog, we spawn exactly at the same place.

Here is what i wrote :

Code:
local function savePlayerCoords()
    SetTimeout(10000, function()
        TriggerEvent("es:getPlayers", function(users)
                for k,v in pairs(users)do
                    MySQL:executeQuery("UPDATE users SET `lastpos`='@value' WHERE identifier = '@identifier'",
                        {['@value'] = v.pos, ['@identifier'] = v.identifier})
                        end
                    end)
            
            savePlayerCoords()
        end)
    end



And here is a script that writes coordinates into a .txt file and save it in it.

client.lua
RegisterNetEvent("SaveCommand")
AddEventHandler("SaveCommand", function()
x, y, z = table.unpack(GetEntityCoords(GetPlayerPed(-1), true))
local PlayerName = GetPlayerName()
TriggerServerEvent("SaveCoords", PlayerName , x , y , z)
end)

server.lua

Code:
RegisterServerEvent("SaveCoords")
AddEventHandler("SaveCoords", function( PlayerName , x , y , z )
 file = io.open( PlayerName .. "-Coords.txt", "a")
    if file then
        file:write("{" .. x .. "," .. y .. "," .. z .. "},")
        file:write("\n")
    end
    file:close()
end)

AddEventHandler("chatMessage", function(p, color, msg)
    if msg:sub(1, 1) == "/" then
        fullcmd = stringSplit(msg, " ")
        cmd = fullcmd[1]

        if cmd == "/pos" then
        	TriggerClientEvent("SaveCommand", p)
        	CancelEvent()
        end
    end
end)


function stringSplit(self, delimiter)
  local a = self:Split(delimiter)
  local t = {}

  for i = 0, #a - 1 do
     table.insert(t, a[i])
  end

  return t
end


So, i'm trying to save it in my DB that has a
table and in the
column.

I'm struggling with it and don't know how to do that...



Here are some pictures :





 
Last edited: