Jump to content

triggerClientEvent


Anubhav

Recommended Posts

Posted

Hello everyone!,

I want to ask that, how to trigger a event only for a player? It should render something only FOR HIM.

My server side code:

  
addEventHandler("onPlayerLogin", root, function() 
        triggerClientEvent(source,"createGUI", root) -- will it only trigger for him? D: 
        end 
    ) 
  

I'm not showing my full client sided code due to script leakings!

My client side code:

  
function startRendering(forWhom) 
        local whoToRender = forWhom or lp 
        addEventHandler("onClientRender", whoToRender, render) 
    end 
    addEvent("createGUI", true) 
    addEventHandler("createGUI", root, startRendering) 
  

Posted

Another doubt:

  
function startRendering() 
        guiSetVisible(windowOne, true) 
    end 
    addEvent("createGUI", true) 
    addEventHandler("createGUI", root, startRendering) 
  

So will it set the window visible for everyone?

Posted

Depends on who you'll trigger the event. If you call the function startRendering(), it will execute the function for each client.

Posted

Actually, your server-side code above is correct, but the another one is wrong.

Use this one:

function startRendering() 
    addEventHandler("onClientRender", root, render) 
end 
addEvent("createGUI", true) 
addEventHandler("createGUI", root, startRendering) 

It should work.

Posted
If I use guiSetVisible instead of addEventHandler( I removed dx functions. ), will it only be for that plr?

Yes, of course.

But like I said, if you call startRendering without calling it for a specific player, the function will be called for all clients.

Posted

So if I do like this:

  
lp = localPlayer or getLocalPlayer() 
    addEventHandler("onClientPlayerJoin", root, function() 
            startRendering(lp) 
        end 
        ) 
  

correct?

Posted

That function will be called for all players except the local player.

This event is triggered when a player joins a server. It is triggered for all players except the local player, as the local player joins the server before their client-side resources are started.

If that is what you want then it's right.

By the way, lp = localPlayer or getLocalPlayer() is useless. Change it to: lp = localPlayer

Posted

If I do it like this:

SERVER:

[lua]

addEventHandler("onPlayerJoin", root, function()

triggerClientEvent(source, "createGUI", root, source)

end

)

Posted
If I do it like this:

SERVER:

addEventHandler("onPlayerJoin", root, function() 
      triggerClientEvent(source, "createGUI", root, source) 
end 
) 

You can pass the player element as the source of the event instead of an argument.

Posted (edited)
  
  
  
addEventHandler("onPlayerLogin", root, function() 
  
        triggerClientEvent(source,"createGUI", source) -- due to first argument, this will trigger for 1 player and 3rd argument is for specifying source of the évent  
        end 
    ) 
  

  
local function startRendering () 
dxDrawText(...) 
end 
  
addEvent("createGUI",true) 
    addEventHandler("createGUI", root, 
     function() 
      addEventHandler("onClientRender", root, startRendering ) 
        end 
        ) 
  
  
  

EDIT: I changed the event name to "createGUI". I have no idea how I missed this :D

Edited by Guest
Posted
Stuff.
This event is triggered when a player joins a server. It is triggered for all players except the local player

As soon as a client joins the server, you just can use 'onClientResourceStart' (Remember to use the resource-root, not root! (Otherwise it will start by every resource that gets started)) or put: functionname() to start the function. Why? Because when she/he joins, the scripts get started.

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