[Help] Saving pos to DB

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. :wink:

__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]

Old manifest version, why?

Is your resource called essentialmode? If not, why are you loading from this?

Neither script has defined a variable called user.

@element.93

Woops for the old manifest version. Followed a simple create-a-__manifest.lua guide, looks like it is outdated. Thanks for showing me!

I require this because I want to interact with the database, essentialmode has that MySQL independency and I’m using that. Do I need to copy these files over to my ressource aswell? Can’t I use that one, just like they use this one?

  1. Isn’t user.identifier a global array of information?
    This is essentialmode’s official page:

Thanks!
N3xty6

No, it’s a table that’s passed to the essentialmode’s events. It’s not “global”.

You’re passing the identifier to the event, use that… I don’t know why you didn’t do it for this line as you’ve done it for the query inside the if result[1] statement…

May I suggest you learn how to program, then the syntax of Lua. If you’re serious about creating addons I suggest you learn first, it’ll make this process a lot easier.

@Havoc

  1. What if I use my ressource inside essentialbase? Would that then work? I suppose it would?

  2. True. I should’ve seen it! But do you have any idea why my querries are not ran by the server?

Yes, I should learn programming first, but I like learning from other scripts, that’s how I learn how to code SQF. So if you don’t mind, I prefer your comments on how I should fix my code and where to look rather than a life lesson :smiley: (No offense!)

Cheers,
N3xty6

It would but, I seriously suggest you don’t do that. It just keeps thing nice and you don’t have to faff around when it gets updated etc.

this:

Where is user defined? If it’s not, then this value is going to be nil and your queries are going to “fail” at finding a user.

Ok, not the best way to learn programming (you pick up other’s bad habits, etc) but, whatever floats your boat :wink:

@Havoc

What if my ressource is located in the [essential] folder and uses ‘dependency ‘essentialmode’’ ?
By doing this I can simply update essentialmode because I didn’t touch anything from the original files? AND use ‘user.identifier’ ? Or is that ONLY working inside the original essentialmode?

Yes, it’s true that it probably isn’t the best way; but I think it’s a good way since I’m only writing Lua for a little code project for friends and by learning this way I’m having fun :wink:

Little question: is there a way of executing Lua script files directly ingame? Realtime debugging?

Thanks Havoc!

That will make sure that essentialmode is loaded before your addon is. It’ll allow you to do stuff (e.g. require "resources/essentialmode/lib/MySQL") with confidence (you’ll now it definitely exists). And yes, I suggest you do this for the addon :slight_smile:

No. No it won’t. The users table is in the scope of essentialmode (it can only be accessed by them). Your files don’t have access (even if you add the dependency). Like you asked :slight_smile:

Not that I know of. You can however, restart the addon with the rcon command restart <resource>.

@Havoc

Awesome! I got my script working, with some things I want to change but it basically saves the position to the DB.

  1. I changed ‘user.identifier’ to ‘GetPlayerServerId(PlayerId())’.
  2. It’s spamming the console so I changed the loop delay from 100 ms to 10000 ms.
  3. I’m now writing in the playerSpawned eventHandler the function that will load that position from the DB and apply it to the player. I probably will have to do a check to see if the player isn’t RE-spawning after dying!

Thanks for the tip, I’ll definately use that restart ressource command.
N3xty6

I have the backup system but how do you make the player spawn on the database

@cjeje84_cjejej84

What do you mean ‘spawn on the database’?
Save the player’s position IN the database?

Do not spawn the player through the database

@cjeje84_cjejej84

Still have no idea what you’re talking about… :confused:
‘do not spawn player through the database’ means that I shouldn’t do it?

No, not that.

Players will spawn through the database when they go to connect to the server how do you

AddEventHandler (‘es: spawnPlayer’, function (x, y, z, model)
Exports.spawnmanager: spawnPlayer ({x = x, y = y, z = z, model = GetHashKey (model)})
End)

@cjeje84_cjejej84

I’m still working on that, sorry, once I have it i’ll probably post it here

Thank you I’m nice I’ll wait

Hello, it’s possible to add your new code ?

i try with your code :slight_smile:

@Blawdi

It’s a help thread, it’s not fully working… I’m still working on it

hey :slight_smile: you relase the script if is work ? :stuck_out_tongue:

I try to spawn the player by the database but I have a black screen

RegisterServerEvent(‘playerSpawn’)
AddEventHandler(‘playerSpawn’, function()
local player = GetPlayerIdentifiers(source)[1]
local executed_query = MySQL:executeQuery(“SELECT * FROM posjoueur WHERE identifier = ‘@identifier’”, {[’@identifier’] = player})
local result = MySQL:getResults(executed_query, {‘pos_x’, ‘pos_y’, ‘pos_z’}, “identifier”)

if(result)then
	for k,v in ipairs(result)do
  print(v.x)
  pos_x = v.x
  pos_y = v.y
  pos_z = v.z
  end
end

TriggerClientEvent('es_roleplay:spawnPlayer', source, pos_x, pos_y, pos_z)

end)