Jump to content

Problem - Login Window


Ice_Cool

Recommended Posts

Hello!

I am currently Learning MTA:SA scripting.

Basically i wrote login panel script and tried run it but login window can not disable on my screen it's work fine from console

But it's work fine if i copy those codes and past in notebook++ and try....

https://wiki.multitheftauto.com/wiki/Int ... ng_the_GUI here's the link...

But when i write it by hands it don't works i checked everything goes fine but i don't know why it's not working...Have any idea?

Thanks!

Link to comment
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, "", true, wdwLogin) 
    Y = 0.5 
    edtPass = guiCreateEdit(X, Y, Width, Height, "", 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 clientSubmitLogin(button,state) 
    if button == "left" and state == "up" then 
    local username = guiGetText(edtUser) 
    local password = guiGetText(edtPass) 
     
    if username and password then 
        triggerServerEvent("submitLogin", getRootElement(), username, password) 
         
        guiSetInputEnabled(false) 
        guiSetVisible(wdwLogin, False) 
        showCursor(false) 
    else 
     
        outputChatBox("Please enter a username and password.") 
        end 
    end 
end 
function loginHandler(username,password) 
    if username == "user" and password == "apple" then 
     
        if (client) then 
            spawnPlayer(client, 1959.55, -1714.46, 10) 
            fadeCamera(client, true) 
            setCameraTarget(client, client) 
            outputChatBox("Welcome to My server.", client) 
        end 
    else 
        outputChatBox("Invalid username and password. Please re-reconnect and try again.", client) 
    end 
end 
  
addEvent("submitLogin",true) 
addEventHandler("submitLogin", root, loginHandler) 

And showCursor i fixed but still having problem :/

Link to comment

Here's the code which I made for someone.

---CLIENT SIDE 
  
showCursor(true) 
m_window = guiCreateWindow(0.63, 0.01, 0.37, 0.46, "Login panel", true) 
guiWindowSetMovable(m_window, false) 
guiWindowSetSizable(m_window, false) 
guiSetAlpha(m_window, 1.00) 
  
m_label1 = guiCreateLabel(5, 22, 283, 15, "If you dont have account yet, create it now!", false, m_window) 
guiSetFont(m_label1, "default-bold-small") 
guiLabelSetHorizontalAlign(m_label1, "center", false) 
guiLabelSetVerticalAlign(m_label1, "center") 
  
m_button1 = guiCreateButton(15, 103, 263, 22, "LOGIN", false, m_window) 
guiSetProperty(m_button1, "NormalTextColour", "FFAAAAAA") 
  
m_label2 = guiCreateLabel(17, 79, 83, 14, "Password", false, m_window) 
guiLabelSetVerticalAlign(m_label2, "center") 
  
m_label3 = guiCreateLabel(17, 55, 83, 14, "Login", false, m_window) 
guiLabelSetVerticalAlign(m_label3, "center") 
  
m_edit1 = guiCreateEdit(78, 56, 200, 20, "", false, m_window) 
m_edit2 = guiCreateEdit(78, 80, 200, 20, "", false, m_window) 
  
m_button2 = guiCreateButton(15, 226, 263, 22, "REGISTER", false, m_window) 
guiSetProperty(m_button2, "NormalTextColour", "FFAAAAAA") 
  
m_label4 = guiCreateLabel(17, 178, 83, 14, "Login", false, m_window) 
guiLabelSetVerticalAlign(label4, "center") 
  
label5 = guiCreateLabel(17, 202, 83, 14, "Password", false, m_window) 
guiLabelSetVerticalAlign(label5, "center") 
  
m_edit3 = guiCreateEdit(78, 178, 200, 20, "", false, m_window) 
m_edit4 = guiCreateEdit(78, 202, 200, 20, "", false, m_window) 
  
guiSetVisible(m_window, true) 
  
addEventHandler("onClientGUIClick", root, 
function (button, state) 
    if button == "left" and state == "up" then 
        if (source == m_button1) then 
            local username = guiGetText(m_edit1) 
            local password = guiGetText(m_edit2)       
            if username and password and username ~= "" and password ~= "" then 
                triggerServerEvent("onRequestLogin", localPlayer, username, password) 
                guiSetVisible(m_window, false) 
            end 
        end 
    end 
end) 
  
addEventHandler("onClientGUIClick", root, 
function (button, state) 
    if button == "left" and state == "up" then 
        if (source == m_button2) then 
            local username = guiGetText(m_edit3) 
            local password = guiGetText(m_edit4)       
            if username and password and username ~= "" and password ~= "" then 
                triggerServerEvent("onRequestRegister", localPlayer, username, password) 
                guiSetVisible(m_window, true) 
            end 
        end 
    end 
end) 
  
    function createText() 
        dxDrawLine(548, 44, 751, 44, tocolor(255, 255, 255, 255), 1, true) 
        dxDrawLine(501, 155, 792, 155, tocolor(32, 32, 32, 255), 4, true) 
    end 
  
function HandleTheRendering ( ) 
    addEventHandler ( "onClientRender", root, createText ) -- keep the text visible with onClientRender. 
end 
  
addEventHandler ( "onClientResourceStart", resourceRoot, HandleTheRendering )    
     
function hideLoginWindow() 
    showCursor(false) 
    removeEventHandler( "onClientRender", root, createText )  
    removeEventHandler("onClientGUIClick",m_button2,clientSubmitRegister) 
    removeEventHandler("onClientGUIClick",m_button1,clientSubmitLogin) 
end 
addEvent("hideLoginWindow", true) 
addEventHandler("hideLoginWindow", getRootElement(), hideLoginWindow) 
  

--SERVER SIDE 
  
function loginPlayer(username,password) 
    if not (username == "") then 
        if not (password == "") then 
            local account = getAccount ( username, password ) 
            if ( account ~= false ) then 
                logIn (source, account, password) 
                triggerClientEvent (source,"hideLoginWindow",getRootElement()) 
                    end 
                    end 
        else 
            outputChatBox ("#0000FF* #FFFFFFError! Please enter your password or username",source,255,255,255,true) 
    end 
    end 
     
addEvent("onRequestLogin", true) 
addEventHandler("onRequestLogin", getRootElement(), loginPlayer) 
  
-- Registration here 
function registerPlayer(username,password) 
    if not (username == "") then 
        if not (password == "") then 
                    local account = getAccount (username,password) 
                    if (account == false) then 
                        local accountAdded = addAccount(tostring(username),tostring(password)) 
                        if (accountAdded) then 
                            outputChatBox ("#0000FF* #FFFFFFYou have sucessfuly registered! [username: #ABCDEF" .. username .. " #FF0000| #FFFFFFPassword: #ABCDEF" .. password .. "#FFFFFF]",source,255,255,255,true ) 
                            setTimer(outputChatBox,800,1,"#0000FF* #FFFFFFYou can now login with your new account.",source,255,255,255,true ) 
                        else 
                            outputChatBox ("#0000FF* #FFFFFFAn unknown error has occured! Please choose a different username/password and try again.",source,255,255,255,true ) 
                        end 
                    else 
                        outputChatBox ("#0000FF* #FFFFFFError! An account with this username already exists!",source,255,255,255,true ) 
                    end 
        else 
            outputChatBox ("#0000FF* #FFFFFFError! Please enter a password!",source,255,255,255,true) 
        end 
    else 
        outputChatBox ("#0000FF* #FFFFFFError! Please enter a username you would like to register with!",source,255,255,255,true) 
    end 
end 
  
addEvent("onRequestRegister", true) 
addEventHandler("onRequestRegister", getRootElement(), registerPlayer) 

Link to comment
  • Moderators

Try this:

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) 
  
    -- 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) 
  
    -- make the window invisible 
    guiSetVisible(wdwLogin, false) 
     
    addEventHandler("onClientGUIClick", btnLogin, clientSubmitLogin, false) 
end 
  
addEventHandler("onClientResourceStart", getResourceRootElement(getThisResource()),  
    function () 
        -- create the log in window and its components 
        createLoginWindow() 
  
        -- 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 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 

Server:

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 to My Server.", 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) 

Make sure meta.xml is correct.

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