Jump to content

Login GUI


Norhy

Recommended Posts

No errors, warning. When i come online no GUI Appear. Something wrong?:

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) 
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 createLoginWindow() 
    -- create all our GUI elements 
    ... 
  
    -- now add our onClientGUIClick event to the button we just created 
    addEventHandler("onClientGUIClick", btnLogin, clientSubmitLogin, false) 
     
    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:

-- create our loginHandler function, with username and password parameters (passed from the client gui) 
function loginHandler(username,password) 
  
end 
  
-- define our custom event, and allow it to be triggered from the client ('true') 
addEvent("submitLogin",true) 
-- add an event handler so that when submitLogin is triggered, the function loginHandler is called 
addEventHandler("submitLogin",root,loginHandler) 
  
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) 

Meta.xml:


PS: Yep, i used Wiki.

Link to comment
<script src="login/gui.lua" type="client" /> 
<script src="login/login.lua" type="server" /> 

this may help..

I don't think the same.. When you not define the type, it will be server-side. Then, he don't specify the type, the type will be Server.

Link to comment

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) 
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 
-- now add our onClientGUIClick event to the button we just created 
addEventHandler("onClientGUIClick", btnLogin, clientSubmitLogin, false) 
  

Server:

  
-- define our custom event, and allow it to be triggered from the client ('true') 
addEvent("submitLogin",true) 
  
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 
-- add an event handler so that when submitLogin is triggered, the function loginHandler is called 
addEventHandler("submitLogin",root,loginHandler) 
  

Meta.xml:


Link to comment

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) 
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 == "down" then 
        -- get the text entered in the 'username' field 
        local user = guiGetText(edtUser) 
        -- get the text entered in the 'password' field 
        local pass = guiGetText(edtPass) 
  
        -- if the username and password both exist 
        if user and pass then 
            -- trigger the server event 'submitLogin' and pass the username and password to it 
            triggerServerEvent("submitLogin", localPlayer, user, pass) 
  
            -- 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 
-- now add our onClientGUIClick event to the button we just created 
addEventHandler("onClientGUIClick", btnLogin, clientSubmitLogin, false) 
  

Server:

  
-- define our custom event, and allow it to be triggered from the client ('true') 
addEvent("submitLogin",true) 
  
function loginHandler(player,username,password) 
    local account = getAccount ( username, password ) 
     if account ~= false then 
         if logIn ( player, username, password ) == true then 
            spawnPlayer(player, 1959.55, -1714.46, 10) 
            fadeCamera(player, true) 
            setCameraTarget(player,player) 
            outputChatBox("Welcome to My Server.", player) 
            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.",player) 
    end 
end 
-- add an event handler so that when submitLogin is triggered, the function loginHandler is called 
addEventHandler("submitLogin",root,loginHandler) 
  

Edited by Guest
Link to comment

Client:

  
wdwLogin = guiCreateWindow(0.375, 0.375, 0.25, 0.25, "Please Log In", true) 
guiCreateLabel(0.0825, 0.2, 0.25, 0.25, "Username", true, wdwLogin) 
guiCreateLabel(0.0825, 0.5, 0.25, 0.25, "Password", true, wdwLogin) 
  
edtUser = guiCreateEdit(0.415, 0.2, 0.5, 0.15, "", true, wdwLogin) 
edtPass = guiCreateEdit(0.415, 0.5, 0.5, 0.15, "", true, wdwLogin) 
guiEditSetMaxLength(edtUser, 50) 
guiEditSetMaxLength(edtPass, 50) 
  
btnLogin = guiCreateButton(0.415, 0.7, 0.25, 0.2, "Log In", true, wdwLogin) 
  
guiSetVisible(wdwLogin, false) 
  
addEventHandler("onClientResourceStart", resourceRoot,  
    function () 
         guiSetVisible(wdwLogin, true) 
         showCursor(true) 
         guiSetInputEnabled(true) 
    end 
) 
  
addEventHandler('onClientGUIClick',root, 
        function() 
                 if source == btnLogin then 
                     local user = guiGetText(edtUser) 
                     local pass = guiGetText(edtPass) 
  
                         if user and pass then 
                             triggerServerEvent('submitLogin',localPlayer,user,pass) 
                             guiSetInputEnabled(false) 
                             showCursor(false) 
                             guiSetVisible(wdwLogin,false) 
                         else 
                             outputChatBox("Please enter a username and password.") 
                         end 
               end 
     end 
) 
  

Server:

  
addEvent("submitLogin",true) 
  
function loginHandler(thePlayer,username,password) 
    local account = getAccount ( username, password ) 
        if account ~= false then 
            logIn ( thePlayer, username, password ) 
            spawnPlayer(thePlayer, 1959.55, -1714.46, 10) 
            fadeCamera(thePlayer, true) 
            setCameraTarget(thePlayer,thePlayer) 
            outputChatBox("Welcome to My Server.", thePlayer) 
        else 
            outputChatBox("Invalid username and password. Please re-connect and try again.",thePlayer) 
    end 
end 
addEventHandler("submitLogin",root,loginHandler) 
  

If it not work now, please wait for any more experienced help you.

Link to comment

I want to make a script. I will explain: When on a Race gamemode, a Hunter kill you then a Message will appear: getPlayerName (So the name of the player who killed you) killed you. Example: Norhy killed you. So when i shoot you with a hunter a message will appear: Norhy killed your, or the name of the Attacker.

function player_Wasted ( ammo, attacker, weapon, bodypart ) 
    if ( attacker ) then 
        local tempString 
        if ( getElementType ( attacker ) == "player" ) then 
            tempString = getPlayerName ( attacker ).." killed "..getPlayerName ( source ).." ("..getWeaponNameFromID ( weapon )..")" 
        elseif ( getElementType ( attacker ) == "vehicle" ) then 
            local tempString = getPlayerName ( getVehicleController ( attacker ) ).." killed "..getPlayerName ( source ).." ("..getWeaponNameFromID ( weapon )..")" 
        end 
        if ( bodypart == 9 ) then 
            tempString = tempString.." (HEADSHOT!)" 
        else 
            tempString = tempString.." ("..getBodyPartName ( bodypart )..")" 
        end 
        outputChatBox ( tempString ) 
    else 
        outputChatBox ( getPlayerName ( source ).." died. ("..getWeaponNameFromID ( weapon )..") ("..getBodyPartName ( bodypart )..")" ) 
    end 
end 
addEventHandler ( "onPlayerWasted", getRootElement(), player_Wasted ) 

How to make that pls?

Link to comment
I want to make a script. I will explain: When on a Race gamemode, a Hunter kill you then a Message will appear: getPlayerName (So the name of the player who killed you) killed you. Example: Norhy killed you. So when i shoot you with a hunter a message will appear: Norhy killed your, or the name of the Attacker.
function player_Wasted ( ammo, attacker, weapon, bodypart ) 
    if ( attacker ) then 
        local tempString 
        if ( getElementType ( attacker ) == "player" ) then 
            tempString = getPlayerName ( attacker ).." killed "..getPlayerName ( source ).." ("..getWeaponNameFromID ( weapon )..")" 
        elseif ( getElementType ( attacker ) == "vehicle" ) then 
            local tempString = getPlayerName ( getVehicleController ( attacker ) ).." killed "..getPlayerName ( source ).." ("..getWeaponNameFromID ( weapon )..")" 
        end 
        if ( bodypart == 9 ) then 
            tempString = tempString.." (HEADSHOT!)" 
        else 
            tempString = tempString.." ("..getBodyPartName ( bodypart )..")" 
        end 
        outputChatBox ( tempString ) 
    else 
        outputChatBox ( getPlayerName ( source ).." died. ("..getWeaponNameFromID ( weapon )..") ("..getBodyPartName ( bodypart )..")" ) 
    end 
end 
addEventHandler ( "onPlayerWasted", getRootElement(), player_Wasted ) 

How to make that pls?

Is this working?

Link to comment
addEventHandler ( "onPlayerWasted", getRootElement( ), 
        function() 
                getElementType ( attacker ) == "player" ) then 
                    outuptChatBox ( getPlayerName ( source ).." died. ) 
        end 
end 
addEventHandler ( "onPlayerWaster, getRootElement(), player_Waster ) 
                 

Tried to make a script when a player kills you a message will appear. Is it working? Cuz i don't have someone to test :P. Please, is the script working?

Link to comment

I'm back!

No. You forgot " on the end of argument 1 ( text ) in outputChatBox.

Try:

addEventHandler('onPlayerWasted',root, 
        function(ammo,attacker,weapon,bodypart) 
          if attacker and attacker ~= source then 
             local car = getPedOccupiedVehicle(attacker) 
               if car then 
                  if getElementModel(car) == 425 then 
                      outputChatBox ( getPlayerName(attacker).." has killed you!",source,255,255,255,false) 
                      outputChatBox ( getPlayerName(attacker).." has killed "..getPlayerName(source),root,255,255,255,false) 
                   end 
              end 
         end 
    end 
) 

I've not tested but I get no error in debug :D

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