Hiding Posted April 7, 2023 Share Posted April 7, 2023 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
Overkillz Posted April 9, 2023 Share Posted April 9, 2023 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. 1 Link to comment
johnm18 Posted April 9, 2023 Share Posted April 9, 2023 Getting country flags through admin is not recommended anymore. Use the 'ip2c' standalone resource that comes with the default resources. 1 Link to comment
Hiding Posted April 10, 2023 Author Share Posted April 10, 2023 (edited) 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.. wtf? Edited April 10, 2023 by Hiding Link to comment
Overkillz Posted April 10, 2023 Share Posted April 10, 2023 (edited) 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.. 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 April 10, 2023 by Overkillz 1 Link to comment
Hiding Posted April 10, 2023 Author Share Posted April 10, 2023 (edited) Thank you so much, it works perfectly now Edited April 10, 2023 by Hiding Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now