Jump to content

Easy Help!


Chaz-CR

Recommended Posts

Hello im trying to do something simple but i cannot. I want to get the account name of the player and put it in a gui, so i made this but doesnt work.

CLIENT

function AccName(thePlayer) 
guiSetText(AccName,tostring(triggerServerEvent("AccountName",resourceRoot))) 
end 
addEventHandler("onClientResourceStart", resourceRoot,AccName) 

SERVER

function AccountName() 
getAccountName ( source ) 
end 
addEventHandler("AccountName",root,AccountName) 
addEvent("AccountName",true) 

Link to comment
Line 2 in the server script should be:
return getAccountName( source ) 

However I'm not sure if triggerServerEvent are able to just return like this but if it works then this should help.

triggerEvent,triggerServerEvent, and triggerClientEvent all return true or false.

I suggest you set a value for the player (using setElementData) when the player logs in to set the data to their username, and then on the client side you can just use getElementData.

Link to comment

Lol.

Why da hell you are triggering it client to server? Trigger it server to client.

Server:

  
function AccountName() 
local acc = getAccountName ( source ) 
if acc then 
triggerClientEvent("AccountName",source,acc) 
     end 
end 
addEventHandler("onPlayerLogin",getRootElement(),AccountName) 
  
  

Client:

  
function AccountName(acc) 
guiSetText(AccName,acc) 
end 
addEventHandler("AccountName",root,AccountName) 
addEvent("AccountName",true) 
  

I think it will work. like this.

Edited by Guest
Link to comment

"onClientResourceStart" triggers when a player just joined the game and all client sided resources where downloaded successfully, at that moment this player isn't logged in so the AccountName() function in Anubhav's script should be triggered from the server sided event:

addEventHandler( "onPlayerLogin", getRootElement( ), AccountName ) 

You may also update the client script, line 2 again.

function AccountName(acc) 

otherwise the variable "acc" would not be defined.

Link to comment
  • Moderators

But if you wanna still use triggers here is what you should have done.

Client ask the server for the account name of the player

--Client 
function askServerForAccName() 
    triggerServerEvent("onAccNameRequest", localPlayer) 
end 

Server gets the account name of the player and send it to the client using another event

--Server 
addEvent("onAccNameRequest", true) 
function sendAccNameToClient() 
    if not client then return end 
    local account = getPlayerAccount( client ) 
    local accName = getAccountName( account ) 
    if accName then 
        triggerClientEvent(client, "onAccNameResponse", client, accName) 
    end 
end 
addEventHandler("onAccNameRequest", root, sendAccNameToClient) 

Client receive the accName through that event and finally set the text of your gui:

addEvent("onAccNameResponse", true) 
function fillGuiWithAccName( accName ) 
    guiSetText(AccName, tostring( accName )) 
end 
addEventHandler("onAccNameResponse", root, fillGuiWithAccName) 

Link to comment

Solved, thanks to yall guys! Now i was wondering how can i upgrade the guiSetText each time the player opens the panel, because i made something like this but with money, so i need that when the player's cash ia changed the guiSetText can be changed as well. Thanks for the support!

Link to comment

I did this but it's still not working, some help?

CLIENT

addEventHandler ( "onClientElementDataChange", getRootElement(), 
function () 
    if getElementType ( source ) == "player" then 
local localPlayerName = getPlayerName(getLocalPlayer()) 
guiSetText(Nickname, localPlayerName) 
guiSetText(TotalEXP, getElementData(localPlayer,"EXP")) 
guiSetText(Level, getElementData(localPlayer,"Level")) 
guiSetText(Clan,localPlayerName) 
guiSetText(BankMoney, getElementData(localPlayer,"Bank")) 
end 
end 
) 

SERVER

addEventHandler ( "onPlayerLogin", root, function ( ) 
for i, v in ipairs ( getElementsByType ( "player" ) ) do 
local acc  = getPlayerAccount ( v ) 
setElementData ( v, "Bank", exports.Banco:getBankAccountBalance(v) ) 
setElementData ( v, "EXP", exports.exp_system:getPlayerEXP(v )) 
setElementData ( v, "Level", exports.exp_system:getPlayerLevel ( v )) 
if not getPlayerTeam ( v ) then return end 
local playerTeam = getPlayerTeam ( v ) 
setElementData ( v, "TeamName", getTeamName ( playerTeam ) ) 
end 
end ) 

Link to comment

Server:

  
function nameOfFunc () 
local localPlayerName = getPlayerName(getLocalPlayer()) 
guiSetText(Nickname, localPlayerName) 
guiSetText(TotalEXP, getElementData(localPlayer,"EXP")) 
guiSetText(Level, getElementData(localPlayer,"Level")) 
guiSetText(Clan,localPlayerName) 
guiSetText(BankMoney, getElementData(localPlayer,"Bank")) 
end 
end 
setTimer(nameOfFunc,1000,0) 
  

Use setTimer . ITS Easy. Remember if you edit the function name edit setTimer function also. It will update every one secon.

Link to comment
addEventHandler ( "onClientElementDataChange", getRootElement(), 
function (dataName) 
    if dataName == "EXP" then 
        guiSetText(TotalEXP, getElementData(source,"EXP")) 
    elseif dataName == "Level" then 
        guiSetText(Level, getElementData(source,"Level")) 
    elseif dataName == "Bank" then 
        guiSetText(Clan, getPlayerName(source)) 
        guiSetText(BankMoney, getElementData(source,"Bank")) 
    end 
end 
) 
  
addEventHandler( "onClientPlayerChangeNick", localPlayer, 
    function (_, newNick) 
        guiSetText(Nickname, newNick) 
    end 
) 

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