Jump to content

[HELP] Returning something from server side to client side


..:D&G:..

Recommended Posts

Hello guys, so I have this function which checks if the player has a sleeping bag placed in the world:

function hasPlayerSleepingBag(thePlayer) 
    if thePlayer then 
        local account = getAccountName(getPlayerAccount(thePlayer)) 
        local result = db:query("SELECT owner FROM sleepingbags") 
        if result then 
            if account == result[1]['owner'] then 
                return true 
            end 
        else 
            return false 
        end 
    end 
end 
addEvent("hasPlayerSleepingBag", true) 
addEventHandler("hasPlayerSleepingBag", getRootElement(), hasPlayerSleepingBag) 

Now, I need to use this function client side, so I made this:

function hasPlayerSleepingBag(thePlayer) 
    triggerServerEvent("hasPlayerSleepingBag", thePlayer, thePlayer) 
end 

The problem is... it doesn't work -.-

I did this quick test:

function teestFunc() 
    if hasPlayerSleepingBag(getLocalPlayer()) then 
        outputChatBox("YES") 
    else 
        outputChatBox("NO") 
    end 
end 
addCommandHandler("test11", teestFunc) 

And it always outputs NO, but server side it outputs YES...

Any ideas?

Thanks.

Link to comment
But you can't do an if on a function mate because it will always return true

You are totally wrong here, i use it a lot times in my own scripting.

[quote name=..:D&G:..]

It will always output no, because the function doesn't return anything (hasPlayerSleepingBag). You have to use triggerClientEvent from the server side and then addEvent.

What do you mean...? I don't get it o.O

He meant that when u trigger server side event, you need get info back from server side event to client side event by using triggerClientEvent. So that is the reason why it outputs NO on client side but on server side outputs YES, because Client side doesnt know that information because you havent returned that correct info back to client side from server side.

Link to comment
But you can't do an if on a function mate because it will always return true

You are totally wrong here, i use it a lot times in my own scripting.

[quote name=..:D&G:..]

It will always output no, because the function doesn't return anything (hasPlayerSleepingBag). You have to use triggerClientEvent from the server side and then addEvent.

What do you mean...? I don't get it o.O

He meant that when u trigger server side event, you need get info back from server side event to client side event by using triggerClientEvent. So that is the reason why it outputs NO on client side but on server side outputs YES, because Client side doesnt know that information because you havent returned that correct info back to client side from server side.

Thank you.

Returns

Returns true if the event trigger has been sent, false if invalid arguments were specified or a client side element was a parameter.

It will always return true if the event was sent. But now in the functions you're not returning anything which causes you giving nil which gives you false value. The other information is given above by the person. Thank you again for explaining it!

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