Jump to content

Login System Help


Recommended Posts

I've been following the GUI tutorial, and am trying to script a very basic login system. I have been trying to link it to the actual accounts system in MTA (like the one /login uses) and am having a lot of issues.

So far, I have the login GUI passing the credentials to the server, and the server is able to check if they are valid or not (but not log them in). The big issue is getting it to pass that user along to the joinHandler function, so they can spawn. I can't seem to figure it out, and every solution I've tried has failed miserably. any help is appreciated.

core.lua (server)

  
  
function spawnHandler() 
    local x = 2028 
    local y = 1545 
    local z = 11 
    local r = 270 
    local s = 167 
    spawnPlayer(source, x, y, z, r, s) 
    fadeCamera(source, true) 
    setCameraTarget(source, source) 
end 
function joinHandler() 
    spawnHandler() 
    newPlayer = getPlayerName(source) 
    outputChatBox("Welcome to John's Test Server, " ..newPlayer.. "!", source, 0, 255, 0, true) 
    outputChatBox( newPlayer.. " has joined the server.", getRootElement(), 255, 0, 0, true) 
end 
function quitHandler(quitType) 
    local quittingPlayerName = getPlayerName(source) 
    outputChatBox(quittingPlayerName .. " has left the server [" .. quitType .. "]" ) 
end 
addEventHandler("onPlayerQuit", getRootElement(), quitHandler) 
--addEventHandler("onPlayerJoin", getRootElement(), joinHandler) 
function loginHandler(username, password) --This is the area where the trouble begins 
   local account = getAccount(username, password) 
        if ( account ~= false ) then  
            outputChatBox("good pass") 
        else 
            outputChatBox ("bad pass") 
        end 
end 
  
addEvent("submitLogin",true) 
addEventHandler("submitLogin",root,loginHandler) 
  

and here is login.lua (clientside)

  
  
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) 
    addEventHandler("onClientGUIClick", btnLogin, clientSubmitLogin, false) 
    guiSetVisible(wdwLogin, false) 
end 
  
addEventHandler("onClientResourceStart", getResourceRootElement(getThisResource()),  
    function () 
        createLoginWindow() 
            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, logger) 
            guiSetInputEnabled(false) 
            guiSetVisible(wdwLogin, false) 
            showCursor(false) 
        else 
            outputChatBox("Please enter a username and password.") 
        end 
    end 
end 
  

Link to comment

server side:

function spawnHandler(player) 
    local x = 2028 
    local y = 1545 
    local z = 11 
    local r = 270 
    local s = 167 
    spawnPlayer(player, x, y, z, r, s) 
    fadeCamera(player, true) 
    setCameraTarget(player, player) 
end 
  
function joinHandler() 
    spawnHandler(source) 
    newPlayer = getPlayerName(source) 
    outputChatBox("Welcome to John's Test Server, " ..newPlayer.. "!", source, 0, 255, 0, true) 
    outputChatBox( newPlayer.. " has joined the server.", getRootElement(), 255, 0, 0, true) 
end 
  
function quitHandler(quitType) 
    local quittingPlayerName = getPlayerName(source) 
    outputChatBox(quittingPlayerName .. " has left the server [" .. quitType .. "]" ) 
end 
addEventHandler("onPlayerQuit", getRootElement(), quitHandler) 
--addEventHandler("onPlayerJoin", getRootElement(), joinHandler) 
  
function loginHandler(username, password) --This is the area where the trouble begins 
   local account = getAccount(username, password) 
        if ( account ~= false ) then 
            outputChatBox("good pass",source,0,255,0) 
            joinHandler() 
        else 
            outputChatBox ("bad pass",source,255,0,0) 
        end 
end 
addEvent("submitLogin",true) 
addEventHandler("submitLogin",root,loginHandler) 

client side:

  
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) 
    addEventHandler("onClientGUIClick", btnLogin, clientSubmitLogin, false) 
    guiSetVisible(wdwLogin, false) 
end 
  
addEventHandler("onClientResourceStart", getResourceRootElement(getThisResource()), 
    function () 
        createLoginWindow() 
            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", getLocalPlayer(), username, password) 
            guiSetInputEnabled(false) 
            guiSetVisible(wdwLogin, false) 
            showCursor(false) 
        else 
            outputChatBox("Please enter a username and password.") 
        end 
    end 
end 

Link to comment

well, at client side you had an error in triggerServerEvent which was giving bad argument,

your error was:

you was triggering some argument called "logger" which didn't exist in the script so i changed it to getLocalPlayer() and got fixed.

in server side you was trying to call a function but your player element wasn't defined so i added some arguments as you can see and thats all.

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