Jump to content

Problem with triggerClientEvent


Xabi

Recommended Posts

Hi all, I have a problem with a client event not being triggered on first server startup, but it does when I disconnect and login back.

It should show the login interface but, as I said, when I first launch the server it doesn't show it (the window is being created) but, if I disconnect from the server and go in again, without restarting neither the server nor the game, it shows the window correctly.

I will leave you the relevant code as it will explain how is done:

function checkPlayerStatus() 
    ... 
            if status == "playable" then 
                triggerClientEvent(source, "onPlayerRequestLogin", source) 
                -- This trigger returns True 
    ... 
end 
  
addEventHandler("onPlayerJoin", getRootElement(), checkPlayerStatus) 

function createLoginWindow() 
    -- This function is called 
    local X = 0.375 
    local Y = 0.375 
    local Width = 0.25 
    local Height = 0.25 
    wdwLogin = guiCreateWindow(X, Y, Width, Height, "Please Log In", true) 
  
    -- define new X and Y positions for the first label 
    X = 0.0825 
    Y = 0.2 
    -- define new Width and Height values for the first label 
    Width = 0.25 
    Height = 0.25 
    -- create the first label, note the final argument passed is 'wdwLogin' meaning the window 
    -- we created above is the parent of this label (so all the position and size values are now relative to the position of that window) 
    guiCreateLabel(X, Y, Width, Height, "Username", true, wdwLogin) 
    -- alter the Y value, so the second label is slightly below the first 
    Y = 0.5 
    guiCreateLabel(X, Y, Width, Height, "Password", true, wdwLogin) 
  
  
    X = 0.415 
    Y = 0.2 
    Width = 0.5 
    Height = 0.15 
    edtUser = guiCreateEdit(X, Y, Width, Height, "", true, wdwLogin) 
    Y = 0.5 
    edtPass = guiCreateEdit(X, Y, Width, Height, "", true, wdwLogin) 
    -- set the maximum character length for the username and password fields to 50 
    guiEditSetMaxLength(edtUser, 50) 
    guiEditSetMaxLength(edtPass, 50) 
  
    X = 0.415 
    Y = 0.7 
    Width = 0.25 
    Height = 0.2 
    btnLogin = guiCreateButton(X, Y, Width, Height, "Log In", true, wdwLogin) 
    addEventHandler("onClientGUIClick", btnLogin, loginButtonSubmited, false) 
  
    -- make the window invisible 
    guiSetVisible(wdwLogin, false) 
end 
  
function showLogin() 
    -- This function is not called on first login, but it does when login the second time 
    -- output a brief welcome message to the player 
            outputChatBox("Welcome to My MTA:SA Server, please log in.") 
  
    -- if the GUI was successfully created, then show the GUI to the player 
        if (wdwLogin ~= nil) then 
            guiSetVisible(wdwLogin, true) 
        else 
        -- if the GUI hasnt been properly created, tell the player 
            outputChatBox("An unexpected error has occurred and the log in GUI has not been created.") 
        end  
  
    -- enable the players cursor (so they can select and click on the components) 
        showCursor(true) 
    -- set the input focus onto the GUI, allowing players (for example) to press 'T' without the chatbox opening 
        guiSetInputEnabled(true) 
end 
  
function loginButtonSubmited(button, state) 
    if button == "left" and state == "up" then 
        -- get the text entered in the 'username' field 
        local username = guiGetText(edtUser) 
        -- get the text entered in the 'password' field 
        local password = guiGetText(edtPass) 
  
        -- if the username and password both exist 
        if username and password then 
            -- trigger the server event 'submitLogin' and pass the username and password to it 
            triggerServerEvent("submitLogin", getRootElement(), username, password) 
  
            -- hide the gui, hide the cursor and return control to the player 
            guiSetInputEnabled(false) 
            guiSetVisible(wdwLogin, false) 
            showCursor(false) 
        else 
            -- otherwise, output a message to the player, do not trigger the server 
            -- and do not hide the gui 
            outputChatBox("Please enter a username and password.") 
        end 
    end 
end 
  
addEvent("onPlayerRequestLogin", true) 
addEventHandler("onPlayerRequestLogin", localPlayer, showLogin) 
addEventHandler("onClientResourceStart", resourceRoot, createLoginWindow) 

Thanks in advance for your help.

Link to comment

You can simply create the panel clientside without the triggers. onClientResourceStart + resourceRoot will do the trick. In case you need join verification:

1. When a player joins, set him into a table

2. onClientResourceStart + resourceRoot + triggerServerEvent so you can trigger server to tell that the player is ready

3. Check if the player is in table table, triggerClientEvent and remove him from the table.

Link to comment
You can simply create the panel clientside without the triggers. onClientResourceStart + resourceRoot will do the trick. In case you need join verification:

1. When a player joins, set him into a table

2. onClientResourceStart + resourceRoot + triggerServerEvent so you can trigger server to tell that the player is ready

3. Check if the player is in table table, triggerClientEvent and remove him from the table.

The problem with this solution is that the server script and client script are in different resources.

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