Jump to content

A Question about server only functions.


Hell-Mate

Recommended Posts

Posted

Hello community,

i just wanna ask about server only functions, as you all know getAccountData is server only function

the ques is, how to use it client side ??

What i mean ?

simply i wanna set a gui label with guiSetText ( label, the define of the account data( at server side ) ) i tried to make it with a local define and failed and tried to remove local and make it not local and also same error which is attempt to concatenate a nil value.

I hope anyone can help me :)

example ..

server

function getnum( thePlayer ) 
local theacc= getPlayerAccount ( thePlayer ) 
dataNum= getAccountData ( theacc, "data", mydata) or 0 
outputChatBox ( "Your data number is: "..tonumber(dataNum), thePlayer, 0,255,0 ) 
end 
addCommandHandler ( "datanum", getnum) 

Client

addEventHandler( "onClientGUIClick", root,  
    function () 
        if source == data.button[3] then 
            guiSetText ( label1, "you data number is : "..tonumber(dataNum) ) 
        end 
    end 
) 
  

Posted

triggering didnt help, same error, as triggering won't get defines from server side it will just start functions on the client event .. anymore answers to my ques ?

Posted
triggering didnt help, same error, as triggering won't get defines from server side it will just start functions on the client event .. anymore answers to my ques ?

Show us your code. If we see that you at least tried. We'll fix it for you.

Posted

Server

function datas () 
local theacc = getPlayerAccount ( source ) 
local mydata = getAccountData ( theacc, "myData", datano ) or 0 
end 
addEvent ("getTheData", true) 
addEventHandler ("getTheData", root, datas) 

Client

addEventHandler( "onClientGUIClick", root,  
    function () 
        if source == data.button[3] then 
        triggerServerEvent( "getTheData", localPlayer) 
            guiSetText ( datalabel, "Your life time iron : "..tonumber(mydata) ) 
        end 
    end 
) 

Posted
--server 
function datas () 
local theacc = getPlayerAccount ( client ) 
local mydata = getAccountData ( theacc, "myData", datano ) or 0 
triggerClientEvent("data",client,client,mydata) 
end 
addEvent ("getTheData", true) 
addEventHandler ("getTheData", root, datas) 
  
--Client 
addEvent("data".true) 
function theData(data) 
data = mydata 
end 
addEventHandler("data",root,theData) 
  
addEventHandler( "onClientGUIClick", root,  
    function () 
        if source == data.button[3] then 
        triggerServerEvent( "getTheData", localPlayer) 
            guiSetText ( datalabel, "Your life time iron : "..tonumber(mydata) ) 
        end 
    end 
) 

Posted

You did some things wrong, you'll have to read about triggering events a bit further - play around with it. Just make some test functions to transmit data back and forth - once you get used to it, it's pretty easy to understand. You also used getAccountData wrong.

Try this;

Server:

function sendTheData_Handler() 
    local theAccount = getPlayerAccount(client); 
    local theData = (getAccountData(theAccount, "myData") or 0); -- When using getAccountData, you don't need a 3rd argument. Only when you set data. 
    if(theData) then 
        triggerClientEvent(client, "sendTheData", root); -- Send the data back to the client. 
    end 
end 
addEvent ("getTheData", true); 
addEventHandler ("getTheData", root, sendTheData_Handler); 

Client:

-- Trigger the server event, which gets the Account Data. 
addEventHandler("onClientGUIClick", root, 
    function() 
        if(source == data.button[3]) then 
            triggerServerEvent("getTheData", localPlayer); 
        end 
    end) 
  
-- This client event will be triggered if data was retrieved without error. The server transmit the data to this function (playerData). 
function retrieveTheData_Handler(playerData) 
    if(playerData ~= nil) then 
        guiSetText(datalabel, "Your life time iron: "..tonumber(playerData)); 
    end 
end 
addEvent("sendTheData", true); 
addEventHandler("sendTheData", root, retrieveTheData_Handler); 

Posted
  
  
addEventHandler( "onClientGUIClick", root,  
  
    function (b,s) 
if b == "left" and state == "up" 
        if source == data.button[3] then 
  
        triggerServerEvent( "getTheData",resourceRoot, localPlayer)-- res root uses less cpu and event will be heard within the same resource. 
  
             
        end 
end 
    end 
  
) 
addEvent ("getTheData", true) 
  
addEventHandler ("getTheData", root,  
function ( d) 
guiSetText ( datalabel, "Your life time iron : "..d) -- you don't need to convert it into a number, string will also work 
end) 
  
  

  
  
function datas (p) 
if p ~= client then return end 
local theacc = getPlayerAccount ( p ) 
local mydata = getAccountData ( theacc, "myData", datano ) or 0 
if not mydata then return end 
triggerServerEvent ( p, "getTheData",p,mydata) 
end 
  
addEvent ("getTheData", true) 
  
addEventHandler ("getTheData", root, datas) 

EDIT: When I finishd writing the code then found that dealman already posted.

  • MTA Team
Posted (edited)
Element data.

Do not use element data to send these kinds of things.

Edited by Guest
Posted
You did some things wrong, you'll have to read about triggering events a bit further - play around with it. Just make some test functions to transmit data back and forth - once you get used to it, it's pretty easy to understand. You also used getAccountData wrong.

Try this;

Server:

function sendTheData_Handler() 
    local theAccount = getPlayerAccount(client); 
    local theData = (getAccountData(theAccount, "myData") or 0); -- When using getAccountData, you don't need a 3rd argument. Only when you set data. 
    if(theData) then 
        triggerClientEvent(client, "sendTheData", root); -- Send the data back to the client. 
    end 
end 
addEvent ("getTheData", true); 
addEventHandler ("getTheData", root, sendTheData_Handler); 

Client:

-- Trigger the server event, which gets the Account Data. 
addEventHandler("onClientGUIClick", root, 
    function() 
        if(source == data.button[3]) then 
            triggerServerEvent("getTheData", localPlayer); 
        end 
    end) 
  
-- This client event will be triggered if data was retrieved without error. The server transmit the data to this function (playerData). 
function retrieveTheData_Handler(playerData) 
    if(playerData ~= nil) then 
        guiSetText(datalabel, "Your life time iron: "..tonumber(playerData)); 
    end 
end 
addEvent("sendTheData", true); 
addEventHandler("sendTheData", root, retrieveTheData_Handler); 

tried it, no error but the label not get set, the reason that there is no error is that u put a check if it nill or not ..

when i removed the check i got the same error.

Posted

No, he is indeed right - I missed that.

A label needs a string, a number is not a string. Therefore you need to convert the number to a string using tostring.

Posted
No, he is indeed right - I missed that.

A label needs a string, a number is not a string. Therefore you need to convert the number to a string using tostring.

it sets it as nil

EDIT: Saml1er code worked fine but it was having some errors and i fixed it.

Thanks, Lock.

Posted
No, he is indeed right - I missed that.

A label needs a string, a number is not a string. Therefore you need to convert the number to a string using tostring.

Well I think this works.

  
local str = ''..1 
  

Now it's "1". : D

@Shadex: Great. I missed getAccountData. I'm sure you fixed it look at dealman's post. Oh well who cares you got it working. :lol:

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