Jump to content

Creating a return function through Server and Client


Recommended Posts

I've created a function in Client side which will return true or false in some case.

Is there any possible way that if a server sided function called that client function "using triggerClientEvent() most probably" to get the returned value from that client function??

For example:

-- Server side

local clientFunctionFlag = triggerClientEvent("blablab", source) 

anything like this?

Link to comment

EXAMPLE:

Server:

  
function greetingHandler ( message ) 
    -- the predefined variable 'client' points to the player who triggered the event and should be used due to security issues 
    outputChatBox ( "The client says: " .. message, client ) 
end 
addEvent( "onGreeting", true ) 
addEventHandler( "onGreeting", root, greetingHandler ) 
  

Client;

  
function greetingCommand ( commandName ) 
    triggerServerEvent ( "onGreeting", localPlayer, "Hello World!" )  
    -- localPlayer instead of root makes the client player the 'source' on the server function, eliminating the need for an additional player argument to be transferred. 
end 
addCommandHandler ( "greet", greetingCommand ) 
  

NOTE: Example is from WIKI.

Link to comment
  • Moderators
I've created a function in Client side which will return true or false in some case.

Is there any possible way that if a server sided function called that client function "using triggerClientEvent() most probably" to get the returned value from that client function??

For example:

-- Server side

local clientFunctionFlag = triggerClientEvent("blablab", source) 

anything like this?

No you can't do that, you have to trigger a client event that will then send back the result to another function on the server side through a triggerServerEvent.

Process:

server function 1 -> client function -> server function 2

Example:

Server:

function func1(thePlayer) 
    triggerClientEvent(thePlayer, "needResult", root) 
end 
addCommandHandler("test", func1) 
  
addEvent("receiveResult", true) 
function func2( result ) 
    outputChatBox("The result is "..tostring(result)) 
end 
addEventHandler("receiveResult", root, func2) 

Client:

addEvent("needResult", true) 
function myFunc() 
    local result = true 
    triggerServerEvent("receiveResult", localPlayer, result) 
end 
addEventHandler("needResult", root, myFunc) 

Regards,

Citizen

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