Hey FiveM community!
I’ve started writing LUA some days ago and I’m encountering some problems: nothing’s happening when I try executing my script to save the players’ pos (x, y, z) to my MySQL DB.
Someone willing to help me out here? Don’t fix my code please, just tell me where to look; I’m still learning how to code LUA, I’ve never really followed any programming lesson and I’ve only played with basic C++ yet.
Thanks!
Added the right ressource in the server’s YAML.
__manifest.lua
resource_manifest_version "77731fab-63ca-442c-a67b-abc70f28dfa5"
server_script 'server.lua'
client_script 'client.lua'
Server.lua
[code] – Loading MySQL Class
require “resources/essentialmode/lib/MySQL”
– MySQL:open(“IP”, “databasename”, “user”, “password”)
MySQL:open(“127.0.0.1”, “gta5_dev”, “root”, “DBPASS”)
RegisterServerEvent(“SaveCoords”)
AddEventHandler(“SaveCoords”, function(identifier , x , y , z)
local executed_query = MySQL:executeQuery(“SELECT * FROM users_pos WHERE identifier = ‘@identifier’”, {[’@identifier’] = user.identifier})
local result = MySQL:getResults(executed_query, {‘pos_x’, ‘pos_y’, ‘pos_z’}, “identifier”)
if (result[1]) then
MySQL:executeQuery("UPDATE users_pos SET `pos_x`='@pos_x',`pos_y`='@pos_y',`pos_z`='@pos_z' WHERE identifier = '@identifier'",
{['@pos_x'] = x, ['@pos_y'] = y, ['@pos_z'] = z, ['@identifier'] = identifier})
else
MySQL:executeQuery("INSERT INTO users_pos (`identifier`, `pos_x`, `pos_y`, `pos_z`) VALUES ('@identifier', '0', '0', '0')",
{['@identifier'] = user.identifier})
end
end)
[/code]
client.lua
[code]AddEventHandler(“playerSpawned”, function(spawn)
Citizen.CreateThread(function()
while true do
Citizen.Wait(100)
x, y, z = table.unpack(GetEntityCoords(GetPlayerPed(-1), true))
local identifier = user.identifier
TriggerServerEvent("SaveCoords", identifier, x, y, z)
end
end)
end)
[/code]