So this is my first time trying to do this on fivem. Im trying to update my text client side with variable from the server updated from my database, All my variables very side come back accurate but when I trigger the client command nothing happens.
client
Citizen.CreateThread(function()
while true do
Citizen.Wait(1)
money("Bank:", pMoney)
xp("XP: ", pXP)
end
end)
AddEventHandler("playerSpawned", function()
TriggerServerEvent("playerAccount", source)
end)
RegisterCommand('updateStats', function()
local source = GetPlayerServerId(PlayerId())
TriggerServerEvent('updatePlayerStats', source)
end)
Server
RegisterNetEvent("updatePlayerStats")
AddEventHandler("updatePlayerStats", function(playerMoney, playerXP, playerLevel)
MySQL.Async.fetchAll('SELECT * FROM accounts WHERE user = @username', {['@username'] = GetPlayerName(source)}, function(result)
local playerMoney = result[1].money
local playerXP = result[1].xp
local playerLevel = result[1].level
pMoney = playerMoney
pXP = playerXP
pLevel = playerLevel
end)
end)
Ive searched through the docs and forums but not of my solutions seem to work.
This is probably the issue you should use PlayerPedId()
@ChieF-TroN Not necessarily, as GetPlayerServerId()
takes a player
not a ped.
This is not required, as source
is always defined inside the server side Event Handler, you just need to call it.
Also you could just move this to the server script, save passing any events at all, since RegisterCommand()
is a server side native as well.
2 Likes
I created the command just to check if the variables her sent over accurately before I make them act on their own. So far the variables arenāt updating. Anything I set the int to initialize as is what is outputted in chat.
This is the entire client side of the text im drawing and trying to update
local pMoney = 1
local pXP = 1
local pLevel = 1
function money(content)
SetTextFont(1)
SetTextProportional(0)
SetTextScale(0.5, 0.5)
SetTextEntry("STRING")
AddTextComponentString(content)
DrawText(0.9,0.2)
end
function xp(content)
SetTextFont(1)
SetTextProportional(0)
SetTextScale(0.5,0.5)
SetTextEntry("STRING")
AddTextComponentString(content)
DrawText(0.9,0.3)
end
Citizen.CreateThread(function()
while true do
Citizen.Wait(1)
money("Bank:", pMoney)
xp("XP: ", pXP)
end
end)
AddEventHandler("playerSpawned", function()
TriggerServerEvent("playerAccount", source)
end)
RegisterCommand('updateStats', function()
local source = GetPlayerServerId(PlayerPedId())
TriggerServerEvent('updatePlayerStats', source)
TriggerEvent('chat:addMessage', {
color = { 255, 0, 0},
multiline = true,
args = {"Me", "Money: "..pMoney}
})
end)
Im still fighting with this
Iām not that experienced with MySQL but what comes to my mind is:
Server side
--mysql start
pMoney = playerMoney
pXP = playerXP
pLevel = playerLevel
--mysql end
should be a table for players like:
local pMoney = {}
local pXP = {}
local pLevel = {}
...
--mysql start
pMoney[source] = playerMoney
pXP[source] = playerXP
pLevel[source] = playerLevel
--mysql end
āsourceā for each player on server since all players are synced with the same āserver.lua / scriptā server side, so need a table (or array if you prefer C# or C++) for players
Send me a PM or whatever and I can send a basic example of what I mean once Iām around PC again
Will do! Iām away from my code atm but I will hit you up. Itās highly appreciated
But it looks like Iām setting the trigger up correctly tho, right?
All the examples I see are always triggering chat messages and that doesnāt help explain much
your client side:
TriggerServerEvent("playerAccount", source)
I believe doesnāt require you to send source like that, itās automatically sent to server side as a āhiddenā variable, then server side you can just get it with the variable āsourceā
Like this:
Client:
function SetCash(data)
TriggerServerEvent('SetCharCash', data)
end
Server:
RegisterNetEvent('SetCharCash')
AddEventHandler('SetCharCash', function(data)
srvCash[source] = data
end)
At least it works for me that way, that source is always sent automatically from client to server via events/functions. BUT Iām not completely sure if you should or shouldnāt send source separately.
Also server side I need to do
local src = source
in MySQL functions in events or I just get source as nil inside the MySQL function/query. No idea why I canāt just use source, so if you run into problems with that stuff, try that. Iām probably missing something simple, maybe someone can explain it
I updated my script but it says im trying to concatenate a nil value so im assuming ive done it wrong and still getting a nil return.
client
function updateStats()
local source = GetPlayerServerId(PlayerPedId())
TriggerServerEvent('updatePlayerStats', playerMoney, playerXP, playerLevel)
pMoney = playerMoney
TriggerEvent('chat:addMessage', {
color = { 255, 0, 0},
multiline = true,
args = {"Me", "Money: "..pMoney}
})
end
server
RegisterNetEvent("updatePlayerStats")
AddEventHandler("updatePlayerStats", function(playerLevel, playerXP, playerLevel)
MySQL.Async.fetchAll('SELECT * FROM accounts WHERE user = @username', {['@username'] = GetPlayerName(source)}, function(result)
playerMoney = result[1].money
playerXP = result[1].xp
playerLevel = result[1].level
end)
end)
client:
local playerMoney = 54345 -- or whatever you have on these variables
local playerXP = 4 -- or whatever you have on these variables
local playerLevel = 13 -- or whatever you have on these variables
...
function updateStats()
TriggerServerEvent('updatePlayerStats', playerMoney, playerXP, playerLevel)
end
server:
playerMoney = {} -- table/array serverside for each player, like a list of all players' money
playerXP = {} -- table/array serverside for each player, like a list of all players' XP
playerLevel = {} -- table/array serverside for each player, like a list of all players' level
...
RegisterNetEvent("updatePlayerStats")
AddEventHandler("updatePlayerStats", function(playerMoney, playerXP, playerLevel)
local src = source
MySQL.Async.fetchAll("SELECT money, xp, level FROM accounts WHERE user = " .. GetPlayerName(src), {}, function(result)
TriggerClientEvent('chat:addMessage', src, { args = { "t: ".. result[1].money .." " .. result[1].xp .." ".. result[1].level} })
print(result[1].money) -- print to server side console to see what values you get with anything server side
playerMoney[src] = result[1].money
playerXP[src] = result[1].xp
playerLevel[src] = result[1].level
TriggerClientEvent('chat:addMessage', src, { args = { "tserverside: ".. playerMoney[src] .." " .. playerXP[src] .." ".. playerLevel[src]} })
end)
end)
Try that, I think I have some mistake in it, not sure but should work
Trying to understand how triggering the events work in general, Im drawing text on the client side. I wanna update the text with variables from the server side.
Do I initialize the money variable on the server side and then
triggerclientevent(āeventnameā, money variable)
and then set the text variable to the money variable?
Found out I should have been triggering a client event instead of a server event, so I was doing it backwards.