Jump to content

Need help with DB


JAY.ANN

Recommended Posts

I have a function that gives a vehicle to player and saves it's data to the vehicle database. 

When I call this function from "onPlayerMarkerHit" - it works correctly but if i try to call it from "addCommandHandler" - it doesn't work. Somehow data isn't saving and I don't know why.

Here's my video about this problem on youtube: 

buy_marker = createMarker( 1036.7772216797, -1133.2785644531, 22, "cylinder", 2 )
addEventHandler( "onPlayerMarkerHit", getRootElement(), function( marker )
    if marker == buy_marker then
    local account = getPlayerAccount( source )
    local account_name = getAccountName( account )
    addPlayerVehicle( account_name, 402, 250000, {255, 255, 255, 255, 255, 255}, {255, 255, 255} )
    end
end)

addCommandHandler( "gveh", function( pl, cmd, login, vehID )
    if pl and cmd and login and vehID then
        if getAccount( login ) then
        local nameveh, cost = getVehicleDataFromTable( vehID )
        addPlayerVehicle( login, vehID, cost, { 255, 255, 255, 255, 255, 255 }, { 255, 255, 255 } )
        end
    end
end)


function addPlayerVehicle( login, vehID, cost, color, light )
local account = getAccount( login )
    if account then
    local rnd = math.random( #boughtVehicleParking )
    positionVeh = toJSON( { boughtVehicleParking[ rnd ][ 1 ], boughtVehicleParking[ rnd ][ 2 ], boughtVehicleParking[ rnd ][ 3 ], boughtVehicleParking[ rnd ][ 4 ], boughtVehicleParking[ rnd ][ 5 ], boughtVehicleParking[ rnd ][ 6 ] } )
    local r1 = color[ 1 ] or 255
    local g1 = color[ 2 ] or 255
    local b1 = color[ 3 ] or 255
    local r2 = color[ 4 ] or 255
    local g2 = color[ 5 ] or 255
    local b2 = color[ 6 ] or 255
    local vehcolor = r1..","..g1..","..b1..","..r2..","..g2..","..b2
    local rhl = light[ 1 ] or 255
    local ghl = light[ 2 ] or 255
    local bhl = light[ 3 ] or 255
    local lights = rhl..","..ghl..","..bhl
    freeID = getFreeVehID()
    local nameveh, vehcost = getVehicleDataFromTable( vehID )
    local handlings = toJSON( getServerVehicleHandling( vehID ) )
    local door = {}
        for i = 1, 6 do
        door[ i ] = 0
        end
    local panel = {}
        for i = 1, 7 do
        panel[ i ] = 0
        end
    doors = toJSON( door )
    panels = toJSON( panel )
    wheels = toJSON( { 0, 0, 0, 0 } )
    local accountPlayer = getAccountPlayer( account )
        if accountPlayer ~= nil and accountPlayer ~= false and getElementType( accountPlayer ) == "player" then
        local xr, yr, zr = getElementRotation( accountPlayer )
        local px, py, pz = getElementPosition( accountPlayer )
        local prot = getPedRotation( accountPlayer )
        local offsetRot = math.rad( prot + 90 )
        local vx = px + 5 * math.cos( offsetRot )
        local vy = py + 5 * math.sin( offsetRot )
        local vz = pz + 5
        local vrot = prot + 90
        veh = createVehicle( vehID, 0, 0, 1000 )
        setElementPosition( veh, vx, vy, pz + 0.5 )
        positionVeh = toJSON( { vx, vy, pz + 0.5, 0, 0, vrot } )
        setElementRotation( veh, xr, yr, vrot )
        setVehicleColor( veh, r1, g1, b1, r2, g2, b2 )
        setVehicleHeadLightColor( veh, rhl, ghl, bhl )
        setVehiclePlateText( veh, "TRANZIT" )
        setServerVehicleHandling( veh )
        setElementData( veh, "Owner", accountPlayer )
        setElementData( veh, "ID", freeID )
        local frontLeft, rearLeft, frontRight, rearRight = getVehicleWheelStates( veh )
        wheels = toJSON( { frontLeft, rearLeft, frontRight, rearRight } )
        local door = {}
            for i = 1, 6 do
            door[ i ] = getVehicleDoorState( veh, i - 1 )
            end
        local panel = {}
            for i = 1, 7 do
            panel[ i ] = getVehiclePanelState( veh, i - 1 )
            end
        doors = toJSON( door )
        panels = toJSON( panel )
        end
    dbExec( dbVehicle, "INSERT INTO VehicleList VALUES( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )", freeID, login, vehID, positionVeh, vehcolor, cost, 1000, "TRANZIT", handlings, doors, panels, wheels, lights )
        if accountPlayer ~= nil and getElementType( accountPlayer ) == "player" then
        updateVehicleInfo( accountPlayer )
        end
    end
end
addEvent( "addPlayerVehicle", true )
addEventHandler( "addPlayerVehicle", root, addPlayerVehicle )
Edited by JAY.ANN
Link to comment
  • Moderators
3 hours ago, JAY.ANN said:

addCommandHandler( "gveh", function( pl, cmd, login, vehID )     if pl and cmd and login and vehID then         if getAccount( login ) then         local nameveh, cost = getVehicleDataFromTable( vehID )

local nameveh, cost = getVehicleDataFromTable( vehID )

Does this function accepts strings or numbers?

Because addCommandHandler will provide the vehID as a string.

local nameveh, cost = getVehicleDataFromTable( tonumber(vehID) )
if not nameveh then return end

 

Please use the code tag for your code, or many forum users will not even attempt to look at your problem :idea:

547226153_Schermafbeelding2022-08-19om19_11_27.png.87c9dfead0835a1a393fd9e67af67098.png

 

Link to comment
1 minute ago, IIYAMA said:
local nameveh, cost = getVehicleDataFromTable( vehID )

Does this function accepts strings or numbers?

Because addCommandHandler will provide the vehID as a string.

local nameveh, cost = getVehicleDataFromTable( tonumber(vehID) )
if not nameveh then return end

 

Please use the code tag for your code, or many forum users will not even attempt to look at your problem :idea:

547226153_Schermafbeelding2022-08-19om19_11_27.png.87c9dfead0835a1a393fd9e67af67098.png

 

Thank you for the advice! It returns 2 values from my table. nameveh is a string and cost is a number.

 

serverVehicleTable = {
[402] = { name = "Nissan 300ZX", cost = 250000 }
}

function getVehicleDataFromTable( id )
local id = tonumber(id)
	if id then
	local vehicleTable = serverVehicleTable[ id ]
		if vehicleTable then
		local nameveh = vehicleTable.name
		local cost = vehicleTable.cost
		return nameveh, cost
		else
		local nameveh = getVehicleNameFromModel( id )
		local cost = 100000
		return nameveh, cost
		end
	end
end

 

Link to comment
  • Moderators
5 minutes ago, JAY.ANN said:

nameveh is a string and cost is a number.

That looks fine.

 

Next up, write the following in both situations to the serverlog. Collect them from the logs and place them here, so that we can compare the input from both situations.

outputServerLog(inspect({freeID, login, vehID, positionVeh, vehcolor, cost, 1000, "TRANZIT", handlings, doors, panels, wheels, lights}))

Code should be placed directly before the dbExec function call.

  • Thanks 1
Link to comment

Anyway, I've updated my code and it still have that wierd bug so the deal is not with getVehicleDataFromTable()
 

addCommandHandler( "gveh", function( pl, cmd, login, vehID )
	if pl and cmd and login and vehID then
	addPlayerVehicle( login, vehID, cost, { 255, 255, 255, 255, 255, 255 }, { 255, 255, 255 } )
	end
end)

 

10 minutes ago, IIYAMA said:

That looks fine.

 

Next up, write the following in both situations to the serverlog. Collect them from the logs and place them here, so that we can compare the input from both situations.

outputServerLog(inspect({freeID, login, vehID, positionVeh, vehcolor, cost, 1000, "TRANZIT", handlings, doors, panels, wheels, lights}))

 

MARKER WAY
{ 1, "YeadSeven", 402, "[ [ 2514.5166015625, 2363.5485839844, 3.8178160190582, 0, 0, 90 ] ]", "255,255,255,255,255,255", 250000, 1000, "TRANZIT", '[ { "suspensionLowerLimit": -0.239999994635582, "engineInertia": 5, "suspensionHighSpeedDamping": 0, "collisionDamageMultiplier": 0.5, "suspensionDamping": 0.119999997317791, "seatOffsetDistance": 0.25, "headLight": "small", "dragCoeff": 2, "centerOfMass": [ 0, 0, -0.1000000014901161 ], "steeringLock": 30, "suspensionUpperLimit": 0.2800000011920929, "suspensionAntiDiveMultiplier": 0.4000000059604645, "turnMass": 4000, "brakeBias": 0.449999988079071, "tractionLoss": 0.8999999761581421, "monetary": 35000, "ABS": false, "suspensionFrontRearBias": 0.5, "percentSubmerged": 85, "tractionBias": 0.5, "numberOfGears": 5, "suspensionForceLevel": 1.200000047683716, "animGroup": 0, "engineAcceleration": 11.19999980926514, "maxVelocity": 200, "mass": 1500, "driveType": "rwd", "modelFlags": 10240, "brakeDeceleration": 11, "handlingFlags": 270532608, "tractionMultiplier": 0.699999988079071, "engineType": "petrol", "tailLight": "small" } ]', "[ [ 0, 0, 0, 0, 0, 0 ] ]", "[ [ 0, 0, 0, 0, 0, 0, 0 ] ]", "[ [ 0, 0, 0, 0 ] ]", "255,255,255" }

COMMAND WAY:
{ 1, "YeadSeven", "402", "[ [ 2514.5166015625, 2363.5485839844, 3.8178160190582, 0, 0, 90 ] ]", "255,255,255,255,255,255", 250000, 1000, "TRANZIT", '[ { "suspensionLowerLimit": -0.239999994635582, "engineInertia": 5, "suspensionHighSpeedDamping": 0, "collisionDamageMultiplier": 0.5, "suspensionDamping": 0.119999997317791, "seatOffsetDistance": 0.25, "headLight": "small", "dragCoeff": 2, "centerOfMass": [ 0, 0, -0.1000000014901161 ], "steeringLock": 30, "suspensionUpperLimit": 0.2800000011920929, "suspensionAntiDiveMultiplier": 0.4000000059604645, "turnMass": 4000, "brakeBias": 0.449999988079071, "tractionLoss": 0.8999999761581421, "monetary": 35000, "ABS": false, "suspensionFrontRearBias": 0.5, "percentSubmerged": 85, "tractionBias": 0.5, "numberOfGears": 5, "suspensionForceLevel": 1.200000047683716, "animGroup": 0, "engineAcceleration": 11.19999980926514, "maxVelocity": 200, "mass": 1500, "driveType": "rwd", "modelFlags": 10240, "brakeDeceleration": 11, "handlingFlags": 270532608, "tractionMultiplier": 0.699999988079071, "engineType": "petrol", "tailLight": "small" } ]', "[ [ 0, 0, 0, 0, 0, 0 ] ]", "[ [ 0, 0, 0, 0, 0, 0, 0 ] ]", "[ [ 0, 0, 0, 0 ] ]", "255,255,255" }

 

Solved - there were an vehicle ID as a string

Thanks a lot for your attention and knowledge!

Edited by JAY.ANN
  • Like 1
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...