Jump to content

can I do triggerEventServer return a value??


AlfaMTA

Recommended Posts

Hi guys!

Again I here to ask your help :P

Well, I trying to do a function in my gamemode to check if a player is VIP or not...

To call this function I use triggerServerEvent

  
triggerServerEvent("checkIfPlayerIsVIP", localPlayer) 
  

And I'm using the triggerServerEvent inside a if, see:

  
if (triggerServerEvent("checkIfPlayerIsVIP", localPlayer)) == true then 
  

In the gamemode I put it:

  
addEvent( "checkIsVIP", true ) 
addEventHandler( "checkIsVIP", root,  
 function () 
     local accName = getAccountName(getPlayerAccount(source)) 
     if isObjectInACLGroup("user.".. accName, aclGetGroup("VIP")) then 
       return true 
     else 
       return false 
     end 
 end) 
  

So I would want that the function in gamemode return a boolean value to use on my if to check if player is VIP or not

I hope you understood my situation!

Thanks for all!

Link to comment

triggerServerEvent returns true if it was succefully sent, false otherwise. You can't do it like that, you either make a triggerClientEvent from server side to client side to return true / false or create an exports function which returns true if a player is in the VIP group false otherwise.

Link to comment
create an exports function which returns true if a player is in the VIP group false otherwise.

Exports are for inter-resource communication, and you cannot call server exports from the client, nor vice-versa.

Edited by Guest
Link to comment
  • Other Languages Moderators

Put this server side:

addEventHandler("onPlayerLogin", root, 
function(_, acc) 
    local accName = getAccountName(acc) 
    if isObjectInACLGroup("user."..accName, aclGetGroup("VIP")) then 
        setElementData(source, "VIP", true) 
    end 
end) 

Then use this client side:

if getElementData(localPlayer, "VIP") == true then 

Lua tag is broken.

Link to comment
create an exports function which returns true if a player is in the VIP group false otherwise.

Exports are for inter-resource communication, and you cannot call server exports from the client, nor vice-versa.

I meant with the triggers, but yea, SetElementData is much better.

Link to comment
Put this server side:

addEventHandler("onPlayerLogin", root, 
function(_, acc) 
    local accName = getAccountName(acc) 
    if isObjectInACLGroup("user."..accName, aclGetGroup("VIP")) then 
        setElementData(source, "VIP", true) 
    end 
end) 

Then use this client side:

if getElementData(localPlayer, "VIP") == true then 

Lua tag is broken.

My question were "How can I do return values through triggerEventServer or triggerEventClient to communicate serverside and clientside???" but your suggestion is a good way to resolve my problem

Nice solution, I'm going to test it!

Thank you!

Link to comment

My question were "How can I do return values through triggerEventServer or triggerEventClient to communicate serverside and clientside???"

You cant return a value in such a way, since it takes time for the server to receive the data and to send it back, and that would mean you would have to freeze the client and wait for answer.

The solution is to use triggerClientEvent server sided in the event that you trigged to trigger back to client.

-- Client sided: 
addEvent("onReceiveVIPData", true) 
addEventHandler("onReceiveVIPData", resourceRoot, 
function(isVIP) 
    -- You are a VIP if isVIP is true 
end) 
  
triggerServerEvent("checkIfPlayerIsVIP", resourceRoot) 
  
  
-- Server sided: 
addEvent("checkIfPlayerIsVIP", true) 
addEventHandler("checkIfPlayerIsVIP", resourceRoot, 
function() 
    -- 'client' variable is set by mta to the player that triggered the event from client-side 
    local accName = getAccountName(getPlayerAccount(client)) 
    local isVIP = false 
    if isObjectInACLGroup("user.".. accName, aclGetGroup("VIP")) then 
        isVIP = true 
    end 
    triggerClientEvent(client, "onReceiveVIPData", resourceRoot, isVIP) 
end) 

But a better solution here would be to use onPlayerLogin and setElementData to the player as VIP, or maybe even set the players team to VIP team.

Edited by Guest
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...