Jump to content

get player country failing


Hiding

Recommended Posts

Hello, I'm trying to get the player's country from admin panel, and use that country in client side, but I always get the error msg: failed to call: 'admin:getPlayerCountry' [string"?"]
Admin is running ofc.

-- SERVER
addEventHandler("onResourceStart", resourceRoot, function()
    local player = source
    local country = exports.admin:getPlayerCountry(player) 
    
    triggerClientEvent(player, "onPlayerCountryReceived", player, country)
end)

-- CLIENT
local myCountry = ""

function onPlayerCountryReceived(country)
    myCountry = country 
end
addEvent("onPlayerCountryReceived", true)
addEventHandler("onPlayerCountryReceived", localPlayer, onPlayerCountryReceived)

 

Link to comment

The source element of the event onResourceStart is not a player, so, you are triying to get the country of a Resource, obyously this is not going to work.

What you should do ? You need to loop the whole player list by using getElementsyType("player") and inside that loop ask for player country and do the trigger aswell.

Best regards.

  • Like 1
Link to comment
19 hours ago, Overkillz said:

The source element of the event onResourceStart is not a player, so, you are triying to get the country of a Resource, obyously this is not going to work.

What you should do ? You need to loop the whole player list by using getElementsyType("player") and inside that loop ask for player country and do the trigger aswell.

Best regards.

Thank you for your response. This is the server side now: 
 

addEventHandler("onResourceStart", resourceRoot, function()
    local players = getElementsByType("player")
    for i, player in ipairs(players) do
        local country = exports.admin:getPlayerCountry(player)
        triggerClientEvent(player, "onPlayerCountryReceived", resourceRoot, country)
    end
end)

And this is the client side: 
 

local myCountry = ""

addEvent("onPlayerCountryReceived", true)
addEventHandler("onPlayerCountryReceived", resourceRoot, function(country)
    myCountry = country 
end)

But it says that server triggered client side event onPlayerCountryReceived, but event is not added client side.. :D wtf?

Edited by Hiding
Link to comment
3 hours ago, Hiding said:

Thank you for your response. This is the server side now: 
 

addEventHandler("onResourceStart", resourceRoot, function()
    local players = getElementsByType("player")
    for i, player in ipairs(players) do
        local country = exports.admin:getPlayerCountry(player)
        triggerClientEvent(player, "onPlayerCountryReceived", resourceRoot, country)
    end
end)

And this is the client side: 
 

local myCountry = ""

addEvent("onPlayerCountryReceived", true)
addEventHandler("onPlayerCountryReceived", resourceRoot, function(country)
    myCountry = country 
end)

But it says that server triggered client side event onPlayerCountryReceived, but event is not added client side.. :D wtf?

Thats probably because server-side is running before client side, so, in this case, you should make a trigger from the client to the server and send the response back to the client again. Not sure if there is a better option for it cuz using a timer is not a good idea aswell ...

So here goes my solution:

-- ## CLIENT-SIDE
local myCountry = ""

addEvent("onPlayerCountryReceived", true)
addEventHandler("onPlayerCountryReceived", root, function(country)
    outputChatBox(country)
    myCountry = country 
end)

addEventHandler("onClientResourceStart", resourceRoot, function ()
    triggerServerEvent("onRequestPlayerCountry", resourceRoot)
end)

-- ## SERVER-SIDE
addEvent("onRequestPlayerCountry", true)
addEventHandler("onRequestPlayerCountry", resourceRoot, function()
    local country = exports.admin:getPlayerCountry(client)
    triggerClientEvent(client, "onPlayerCountryReceived", resourceRoot, country)
end)

 

Edited by Overkillz
  • 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...