Jump to content

problems whit: Introduction to Scripting the GUI


DLmass

Recommended Posts

CLIENT:

function createLoginWindow() 
    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) 
  
    
    X = 0.0825 
    Y = 0.2 
    Width = 0.25 
    Height = 0.25 
     
    guiCreateLabel(X, Y, Width, Height, "Username", true, wdwLogin 
    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, "username", true, wdwLogin) 
    Y = 0.5 
    edtPass = guiCreateEdit(X, Y, Width, Height, "password", true, wdwLogin) 
    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) 
  
    guiSetVisible(wdwLogin, false) 
end 
addEventHandler("onClientResourceStart", getResourceRootElement(getThisResource()),  
  
    function () 
        createLoginWindow() 
                 
             outputChatBox("Welcome to My MTA:SA Server, please log in.") 
  
            if (wdwLogin ~= nil) then 
            guiSetVisible(wdwLogin, true) 
        else 
            
            outputChatBox("An unexpected error has occurred and the log in GUI has not been created.") 
            end  
  
            showCursor(true) 
        
            guiSetInputEnabled(true) 
    end 
) 
function createLoginWindow() 
    -- create all our GUI elements 
    ... 
  
    -- now add our onClientGUIClick event to the button we just created 
    addEventHandler("onClientGUIClick", btnLogin, clientSubmitLogin, true) 
     
    function clientSubmitLogin(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 
) 
function loginHandler(username,password) 
    -- check that the username and password are correct 
    if username == "user" and password == "apple" then 
        -- the player has successfully logged in, so spawn them 
        if (client) then 
            spawnPlayer(client, 1959.55, -1714.46, 10) 
            fadeCamera(client, true) 
                        setCameraTarget(client, client) 
            outputChatBox("Welcome", client) 
        end 
    else 
        -- if the username or password are not correct, output a message to the player 
        outputChatBox("Invalid username and password. Please re-connect and try again.",client) 
        end          
end 
  
addEvent("submitLogin",true) 
addEventHandler("submitLogin",root,loginHandler) 

SERVER:

function clientSubmitLogin(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 
function loginHandler(username,password) 
    -- check that the username and password are correct 
    if username == "user" and password == "apple" then 
        -- the player has successfully logged in, so spawn them 
        if (client) then 
            spawnPlayer(client, 1959.55, -1714.46, 10) 
            fadeCamera(client, true) 
                        setCameraTarget(client, client) 
            outputChatBox("Welcome.", client) 
        end 
    else 
        
              outputChatBox("Invalid username and password. Please re-connect and try again.",client) 
        end          
end 
addEvent("submitLogin",true) 
addEventHandler("submitLogin",root,loginHandler) 

The logingui is created as it should, but when i click on Login nothing happends...... what should i do i really need help :cry:

Link to comment

1. use [lua] tags! it's even said on a sticky -.-

2. WRONG CLIENT:

addEventHandler("onClientGUIClick", btnLogin, clientSubmitLogin, true) 
     
    function clientSubmitLogin(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 

3. FIXED CLIENT:

  
     
    function clientSubmitLogin(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 
  
addEventHandler("onClientGUIClick", btnLogin, clientSubmitLogin, true) 
  

This because...

Note: You have to add the command/event handler AFTER you defined the handler function, else it can't find it. The order of execution matters.
Link to comment

add it in the same function, 'below' where u define the button.

btnLogin = guiCreateButton(X, Y, Width, Height, "Log In", true, wdwLogin)

addEventHandler("onClientGUIClick", btnLogin, clientSubmitLogin, true)

Remember to check what functions are client only too, in you original post you have clientside functions in your

serverside script.. these wont work.

Link to comment
can you explain more like on (lua)

in your script.. the addEventHandler line needs to be below/after the createButton line.

in your script it would look like this:

     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, clientSubmitLogin, true) 

i dont know what else it is u want us to tell u :)

if this is too hard to follow, maybe you should think about making an easier one first.

maybe start with just a clientsided script, then once u get it working, add more to the gui.

once u get that all working, u can a server side script to it.

Hope this helps.

what you have pasted here is still quite simple but is has a lot of mixed functions in it...

if u need more lua explaining, u should read all the info related to each function on the wiki first.

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