A function in a function

Hey guys,
I’ve some problems with my script. Basically I want to save vehicles into a MySQL databse when they’re bought over a npc. When the player buy a car, the information will be send over a datastream to the server. My problem is that I call a function in a function and it will be called, but in a wrong order.

So here’s my code:
(I’m leaving the unimportant things off)

function VehicleBuyHandler(sender, handler, id, encoded, decoded)
local SpawnCount = DB.CountCarSpawns()
print("SpawnCount-variable: "…tostring(SpawnCount))
datastream.Hook(“RP_VehicleBuy”, VehicleBuyHandler)

Output: “SpawnCount-variable: nil”

function DB.CountCarSpawns(callback)
DB.Query("SELECT COUNT(*) AS count FROM bprp_carspawnpos WHERE map = " … sql.SQLStr(game.GetMap()) … “;”, function®
if callback then
return tonumber(r[1].count)

When the “DB.CountCarSpawns” function will be called it stops before the “DB.Query” function will be called and jumps back to the “VehicleBuyHandler” function.
At the end of the “VehicleBuyHandler” function it jumps back to the “DB.CountCarSpawns” function and starts the query function.
I have no idea what I should do to force it to call the “DB.Query” function directly.

Any ideas?

Thanks in advance for any help!

The first problem there is that you are using datastream.

Use console commands and usermessages.

You can’t use usermessages to send information from client to server. The wiki says that (quote: “There is no way to send usermessages from client to server.”).
And concommands are not suitable for that function because any client can use it everytime from his console.
What speaks against datastreams? In my opinion, datastreams are perfect for that job.

You are an idiot and have no idea what you are talking about

Then explain me why I shouldn’t use datastreams…

datastream uses console commands for client to server, its fine to use console commands as long as you do checks.

Well dont do clientside final checks, do all checks on the server and dont say i cant do checks in this situation cause you always can.

Datastream is the lazy expensive alternate for usermessages and console commands.

Okay let’s say I want to use datastreams. Can we focus on my main problem, please?
(I can edit this datastream problem later)

No, putting it off is not an option.

Learn the proper way from the beginning or you get in the wrong habits.
We may seem harsh but this is for your own good.

Datastream is like using unchecked console commands its just as easy to exploit

Seen as im in a nice mood ill give you an example of redoing the system.

[lua]local pmeta = FindMetaTable(“Player”)

Usage: Your MySQL Server Info
Extra Info: If You Cant Make The Tables, Dont Do This

local DB_IP = “”
local DB_USER = “”
local DB_PASS = “”
local DB_TABLE = “”
function DatabaseConnect()
database, error = mysql.connect(DB_IP,DB_USER,DB_PASS,DB_TABLE)
if database == 0 then print("[SQL] MySQL Error: “…error) end
print(”[SQL] MySQL Conencted")

Usage: Player(Entity):SetupCars()
Extra Info: Setups the players basic car tables
Returns: Table setup Successful

function pmeta:SetupCars()
local out, error = mysql.query(database, “INSERT INTO cars (steamid,raw) VALUES(’”…self:SteamID()…"’,’’)")
if out then self:PrintMessage(HUD_PRINTTALK, “[SQL] Setup Your Car Database Successful”) return true end
self:PrintMessage(HUD_PRINTTALK, “[SQL] Setup Your Car Database Unsuccessful - Tell An Admin”)
return false

Usage: Player(Entity):CarsTableExists()
Extra Info: Checks if the car table exists
Returns: If Table Exists

function pmeta:CarsTableExists()
local out, error = mysql.query(database, “SELECT * FROM raw WHERE steamid = '”…self:SteamID()…"’")
if out then
return true
return false

Usage: Player(Entity):AddCar(CarTable(Table))
Extra Info: The Car Should Really Be A Shared Table For Usefulness - Consult The Bottom Of Script For Example
Returns: If Added Successfully

function pmeta:AddCar(table)
local old, error = mysql.query(database, “SELECT raw FROM cars WHERE steamid = '”…self:SteamID()…"’")
if !old then return false end
local new, error = mysql.query(database, “UPDATE cars SET raw = '”…old…";"…tostring(table.ID)…"'WHERE steamid = ‘"…self:SteamID()…"’")
if new then return true end
return false

Usage: Player(Entity):CarTable()
Extra Info: Returns Your Car Table For The Entity
Returns: The Table Of Car ID’s

function pmeta:CarTable()
local out, error = mysql.query(database, “SELECT raw FROM cars WHERE steamid = '”…self:SteamID()…"’")
if !out then return {} end
return string.Explode(";", out[1][1])

Tell me if you want me to carry on adding more features.
PS: Requires MySQL Module.