Jump to content

exports.sql:function in client-side


trux_yt

Recommended Posts

hi, how i can use the function 

local sql = exports.sql:query_assoc_single("SELECT nivel FROM characters WHERE characterID = "..exports.players:getCharacterID(getLocalPlayer))

in the client-side? is for a hud, for example.

dxDrawText("LVL: "..tostring(sql.nivel) .."", x*2079, y*185, x*21, y*14, tocolor(255, 255, 255, 255), 1.00, font, "center", "center", false, false, false, false, false)

 

Link to comment

You can't do this on client side and I don't recommend doing it. it's good to stay on server side, use "triggerServerEvent" and "triggerClientEvent" to do this

example:

server:

addEvent("requestGetLevelPlayer", true) --create event on server side
addEventHandler("requestGetLevelPlayer", root,
    function()
       local sql = exports.sql:query_assoc_single("SELECT nivel FROM characters WHERE characterID=?"..exports.players:getCharacterID(source))
       triggerClientEvent(source, "updateLevelPlayer", source, sql.nivel) --send level information to client
    end
)

client:

local level = 0
triggerServerEvent("requestGetLevelPlayer", localPlayer) --send level update request (don't use it in onClientRender)

dxDrawText("LVL: "..level, x*2079, y*185, x*21, y*14, tocolor(255, 255, 255, 255), 1.00, font, "center", "center", false, false, false, false, false)

addEvent("updateLevelPlayer", true) --event to update level
addEventHandler("updateLevelPlayer", root,
    function(playerLevel)
       level = playerLevel --update level
    end
)

 

Edited by Burak5312
Link to comment
On 23/02/2022 at 11:13, Burak5312 said:

You can't do this on client side and I don't recommend doing it. it's good to stay on server side, use "triggerServerEvent" and "triggerClientEvent" to do this

example:

server:

addEvent("requestGetLevelPlayer", true) --create event on server side
addEventHandler("requestGetLevelPlayer", root,
    function()
       local sql = exports.sql:query_assoc_single("SELECT nivel FROM characters WHERE characterID=?"..exports.players:getCharacterID(source))
       triggerClientEvent(source, "updateLevelPlayer", source, sql.nivel) --send level information to client
    end
)

client:

local level = 0
triggerServerEvent("requestGetLevelPlayer", localPlayer) --send level update request (don't use it in onClientRender)

dxDrawText("LVL: "..level, x*2079, y*185, x*21, y*14, tocolor(255, 255, 255, 255), 1.00, font, "center", "center", false, false, false, false, false)

addEvent("updateLevelPlayer", true) --event to update level
addEventHandler("updateLevelPlayer", root,
    function(playerLevel)
       level = playerLevel --update level
    end
)

 

Hi bro, thx, but i have this error in the server code

 

```

attempt to index local sql a boolean value

```

 

in the line 5

Link to comment
7 hours ago, Burak5312 said:
local sql = exports.sql:query_assoc_single("SELECT nivel FROM characters WHERE characterID = "..exports.players:getCharacterID(source))

I may have made a typo here, I fixed it, try this

This works, but, in the client side, how i can made to the level text change the level? 

PlayerLevel What does it perform?

Link to comment

 

You can create a client-side timer with setTimer, then use the triggerServerEvent as is so it will constantly update the level text (1 second may be enough for the update to avoid creating too many network connections)

1 hour ago, trux_yt said:

PlayerLevel What does it perform?

parameter of client side event "updateLevelPlayer"

Edited by Burak5312
Link to comment

Client-side:

local level = 5
	triggerServerEvent("requestGetLevelPlayer", localPlayer) --send level update request (don't use it in onClientRender)
	addEvent("updateLevelPlayer", true) --event to update level
	addEventHandler("updateLevelPlayer", root,
    function(playerLevel)
       level = playerLevel --update level
    end
	)
	dxDrawText("LVL: "..level, x*2079, y*185, x*21, y*14, tocolor(255, 255, 255, 255), 1.00, font, "center", "center", false, false, false, false, false)

Server-side

addEvent("requestGetLevelPlayer", true) --create event on server side
addEventHandler("requestGetLevelPlayer", root,
    function()
       local sql = exports.sql:query_assoc_single("SELECT nivel FROM characters WHERE characterID = "..exports.players:getCharacterID(source))
       triggerClientEvent(source, "updateLevelPlayer", source, sql.nivel) --send level information to client
    end
)

 

Link to comment
6 hours ago, Burak5312 said:

where the event is triggered before the resource starts on the client side or the use of the sql query may be wrong

outputDebugString(sql.nivel)

You can add a debug line inside the event to find out.

I have this error

attempt to index global sql a nil value

 

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