Abbas_gamer100 Posted January 18, 2017 Share Posted January 18, 2017 I've made a Login GUI. The GUI works properly. But it doesn't detect my account. And it says unexpected error has occured which is the message I have set to show when the panel couldn't log me in. The code: addEventHandler("onClientResourceStart", getResourceRootElement(), function() LoginPanel = guiCreateWindow(299, 101, 362, 521, "Login Panel", false) guiWindowSetMovable(LoginPanel, false) guiWindowSetSizable(LoginPanel, false) Image = guiCreateStaticImage(13, 37, 339, 130, ":admin/client/images/flags/ru.png", false, LoginPanel) Label1 = guiCreateLabel(20, 233, 67, 39, "Username", false, LoginPanel) Edit1 = guiCreateEdit(77, 228, 200, 24, "", false, LoginPanel) Edit2 = guiCreateEdit(77, 267, 200, 24, "", false, LoginPanel) Label2 = guiCreateLabel(20, 267, 67, 39, "Password", false, LoginPanel) Button1 = guiCreateButton(20, 334, 104, 40, "Login", false, LoginPanel) addEventHandler("onClientGUIClick", Button1, clientSubmitLogin, false) outputChatBox("Welcome to my Server!") if (LoginPanel ~= nil) then guiSetVisible(LoginPanel, true) else outputChatBox("An unexpected error has occured! Please re-connect and try again.") end showCursor(true) guiSetInputEnabled(true) end ) function clientSubmitLogin(button, state) if button == "left" and state == "up" then local username = guiGetText(Edit1) local password = guiGetText(Edit2) if username and password then triggerServerEvent("submitLogin", getRootElement(), username, password) guiSetInputEnabled(false) guiSetVisible(LoginPanel, false) showCursor(false) else outputChatBox("Please enter a valid username and password.") end end end Server Client's file code : 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) outputDebugString("I am an idiot!") end else outputChatBox("An unexpected error has occured!") end end addEvent("submitLogin", true) addEventHandler("submitLogin", root, loginHandler) Link to comment
AJXB Posted January 18, 2017 Share Posted January 18, 2017 Post the error and the line number, I think I know what's wrong Link to comment
Moderators IIYAMA Posted January 18, 2017 Moderators Share Posted January 18, 2017 (edited) if (client) then 'client' can only be used when an addEventHandler is used. 'client' doesn't exist when you call the function like this: loginHandler("IIYAMA", "PYAMA") Also apply MORE debug lines: (You want to know everything! ) function loginHandler(username, password) outputDebugString("loginHandler has been called") if username == "user" and password == "apple" then outputDebugString("username and password do match!") Solution: Quote https://wiki.multitheftauto.com/wiki/TriggerServerEvent Note: To save server CPU, you should avoid setting theElement to the root element where possible. Using resourceRoot is usually sufficient if the event is handled by the same resource on the server. ? triggerServerEvent("submitLogin", resourceRoot, player, username, password) function loginHandler(player, username, password) if (not client and isElement(player)) or client == player then addEvent("submitLogin", true) addEventHandler("submitLogin", resourceRoot, loginHandler) -- resourceRoot > prevents trigger events from other resources to trigger this event Edited January 18, 2017 by IIYAMA Link to comment
Abbas_gamer100 Posted January 18, 2017 Author Share Posted January 18, 2017 The debugger only says joinHandler has been called. And one more problem occured now. Before when I don't type anything or when I use an account that isn't registered it says please type a valid username and password as I have set it. But now even if I don't write anything it acts as if I written an account and closes the panel and shows the error message. Link to comment
Moderators IIYAMA Posted January 18, 2017 Moderators Share Posted January 18, 2017 Can you show your updated code? Link to comment
Abbas_gamer100 Posted January 18, 2017 Author Share Posted January 18, 2017 Sure. Here it is: addEventHandler("onClientResourceStart", getResourceRootElement(), function() LoginPanel = guiCreateWindow(299, 101, 362, 521, "Login Panel", false) guiWindowSetMovable(LoginPanel, false) guiWindowSetSizable(LoginPanel, false) Image = guiCreateStaticImage(13, 37, 339, 130, ":admin/client/images/flags/ru.png", false, LoginPanel) Label1 = guiCreateLabel(20, 233, 67, 39, "Username", false, LoginPanel) Edit1 = guiCreateEdit(77, 228, 200, 24, "", false, LoginPanel) Edit2 = guiCreateEdit(77, 267, 200, 24, "", false, LoginPanel) Label2 = guiCreateLabel(20, 267, 67, 39, "Password", false, LoginPanel) Button1 = guiCreateButton(20, 334, 104, 40, "Login", false, LoginPanel) addEventHandler("onClientGUIClick", Button1, clientSubmitLogin, false) outputChatBox("Welcome to my Server!") if (LoginPanel ~= nil) then guiSetVisible(LoginPanel, true) else outputChatBox("An unexpected error has occured! Please re-connect and try again.") end showCursor(true) guiSetInputEnabled(true) end ) function clientSubmitLogin(button, state) if button == "left" and state == "up" then local username = guiGetText(Edit1) local password = guiGetText(Edit2) if username and password then triggerServerEvent("submitLogin", resourceRoot, player, username, password) guiSetInputEnabled(false) guiSetVisible(LoginPanel, false) showCursor(false) else outputChatBox("Please enter a valid username and password.") end end end loginHandler: function loginHandler(player, username, password) outputDebugString("loginHandler has been called") if username == "user" and password == "apple" then outputDebugString("Username and password do not match.") if (not client and isElement(player)) or client == player then spawnPlayer(client, 1959.55, -1714.46, 10) fadeCamera(client, true) setCameraTarget(client, client) outputChatBox("Welcome to My Server", client) outputDebugString("I am an idiot!") end else outputChatBox("An unexpected error has occured!") end end addEvent("submitLogin", true) addEventHandler("submitLogin", resourceRoot, loginHandler) Link to comment
Moderators IIYAMA Posted January 18, 2017 Moderators Share Posted January 18, 2017 (edited) ? if username and username ~= "" and password and password ~= "" then -- Is the variable something? + Is it Not an empty string? Edited January 18, 2017 by IIYAMA Link to comment
Abbas_gamer100 Posted January 18, 2017 Author Share Posted January 18, 2017 I didn't understand fully. The thing is. I am a beginner. And I try to make scripts following the MTA tutorials.. And I end up with many bugs and problems. Especially with GUIs I used your modified script. Now the error message doesn't show up but it doesn't log me in either. Link to comment
Abbas_gamer100 Posted January 18, 2017 Author Share Posted January 18, 2017 Update: I kept trying, and I noticed the Console says username and password do not match. But my account exists and the proof is that I used my admin account to restart the script. Link to comment
Moderators IIYAMA Posted January 18, 2017 Moderators Share Posted January 18, 2017 (edited) ? outputDebugString("User: " .. username .. ", password: " .. password) You also need to be a little bit creative. If you are not sure why something(username or password) is NOT equal to .... and ...., then you have to make things visible(outputDebugString). Follow these steps: You start at the beginning of the script. You add debug lines till the end of the script. You run the script. The debugconsole will show you how far the script goes. The part that doesn't get execute is most likely the part that doesn't work. You check that part and fix it. (back to step2) OR post it on the forum and show us: where the code stops / error's / warnings. You get feedback. You edit the code. (back to step2) This way you can get things done! Instead waiting for somebody to SCAN your entire code over and over, which not always gives you result. <<< Not recommended. Edited January 18, 2017 by IIYAMA Link to comment
Abbas_gamer100 Posted January 18, 2017 Author Share Posted January 18, 2017 Alright I will try. And I will report in. Link to comment
Abbas_gamer100 Posted January 18, 2017 Author Share Posted January 18, 2017 (edited) Done. I detected the problem. The GUI panel is working. But it's button functions are not. And they said after executing the working debug strings: attempt to call outputDebugString < a nil value> Here's the code: function clientSubmitLogin(button, state) outputDebugString("The clientSubmitLogin function has been called!") if button == "left" and state == "up" then outputDebugString("Player has called the button function!") local username = guiGetText(Edit1) local password = guiGetText(Edit2) if username and password then --All previous debug strings has been called. And the following debug strings didn't outputDebugscript("Player has logged in!") triggerServerEvent("submitLogin", getRootElement(), username, password) guiSetInputEnabled(false) guiSetVisible(LoginPanel, false) showCursor(false) else outputChatBox("Please enter a valid username and password.") outputDebugString("The function ran through an error while executing!") end end end loginHandler script : --The following debu strings didn't show up too. function loginHandler(player, username, password) outputDebugString("loginHandler has been called") if username == "user" and password == "apple" then outputDebugString("Checking username and password is being executed!") if (isElement(player)) then spawnPlayer(client, 1959.55, -1714.46, 10) fadeCamera(client, true) setCameraTarget(client, client) outputChatBox("Welcome to My Server", client) outputDebugString("Player spawned after logging in!") end else outputChatBox("An unexpected error has occured!") outputDebugString("Player didn't spawn after he logged in!") end end addEvent("submitLogin", true) addEventHandler("submitLogin", resourceRoot, loginHandler) Edited January 18, 2017 by Abbas_gamer100 Link to comment
LoPollo Posted January 18, 2017 Share Posted January 18, 2017 (edited) outputDebugscript ~= outputDebugString Line 9, also i think you didn't copied-pasted the error, since that doesn't match with reality. It's good habit to copy paste errors to prevent these issues, also give us the lines at which the error say the problem is: --error is [2016-10-13 19:47:27] ERROR: someRes\main.lua:35: attempt to concatenate global 'someVar' (a nil value) local somf = function(a,b,c) --this is line 33 statement1() local str = tostring(a*b+c)..someVar outputChatBox(str) return 4 end function asdf() --other code end Then we know line 35 is line 5 in this block, and then we know where to see Edited January 18, 2017 by LoPollo Link to comment
Abbas_gamer100 Posted January 18, 2017 Author Share Posted January 18, 2017 (edited) Alright. Everyting is okay now. I know where exactly the error is. Exactly this time. It's in loginHandler. The GUI runs okay. But it is having a problem loggin me in. The original problem I posted this thread to solve. This is where the script has the error (where the debug strings showed me that it didn't work): loginHandler: function loginHandler(player, username, password) outputDebugString("loginHandler has been called")--This string showed if username == "user" and password == "apple" then outputDebugString("Checking username and password is being executed!")--But this didn't if (isElement(player)) then spawnPlayer(client, 1959.55, -1714.46, 10) fadeCamera(client, true) setCameraTarget(client, client) outputChatBox("Welcome to My Server", client) outputDebugString("Player spawned after logging in!")--And this didn't also. end else outputChatBox("An unexpected error has occured!")--This showed in the server's chat box outputDebugString("Player didn't spawn after he logged in!")--And this showed up. Meaning the GUI runs just fine. But it has a problem logging me in!! end end addEvent("submitLogin", true) addEventHandler("submitLogin", resourceRoot, loginHandler) Edited January 18, 2017 by Abbas_gamer100 Link to comment
Moderators IIYAMA Posted January 18, 2017 Moderators Share Posted January 18, 2017 (edited) and did you try this one? (making data visible) Place it between line 1 and 2. In the code you just posted. (this doesn't solve your problem yet, but it will make it partly visible) ? outputDebugString("User: " .. username .. ", password: " .. password) Edited January 18, 2017 by IIYAMA Link to comment
Abbas_gamer100 Posted January 18, 2017 Author Share Posted January 18, 2017 I think we are almost there. After placing the code you've given me. I had this error message: Error: attempt to concatenate local 'password' (a nil value) Link to comment
Moderators IIYAMA Posted January 18, 2017 Moderators Share Posted January 18, 2017 triggerServerEvent("submitLogin", resourceRoot, player, username, password) You forgot to replace a piece of code. Password is nil because you are only sending 2 arguments instead of 3. triggerServerEvent("submitLogin", resourceRoot, player, username, password) Link to comment
Abbas_gamer100 Posted January 18, 2017 Author Share Posted January 18, 2017 It worked. But the GUI isn't logging me on still. It shows the error message I've set when the GUI couldn't log me in(the original problem). What is the solution for this? Or should I use logIn? If yes tell me how and how to connect it to the GUI. Link to comment
Moderators IIYAMA Posted January 18, 2017 Moderators Share Posted January 18, 2017 Does the outputDebugString shows your username and password correctly? If so then this condition should be OK: if username == "user" and password == "apple" then This function logIn is used when you want to login with for example a GUI instead of the command: /login username password https://wiki.multitheftauto.com/wiki/LogIn If you use this function you won't have to do this: >if username == "user" and password == "apple" then< You do it like this: local account = getAccount ( username ) if account then local successfullyLoggedIn = logIn ( player, account, password ) if successfullyLoggedIn then outputDebugString("yes! Works!") else outputDebugString("Wrong password") end else outputDebugString("Account doesn't exist!") end Link to comment
LoPollo Posted January 18, 2017 Share Posted January 18, 2017 Abbass_gamer100, i didn't followed entirely the thread so can you sum up what's the problem? logIn is a function that is used to bind a player element to an account element. From the serverside script i feel the script more like a "password" for the server, and not an account, so i'm confused. 12 minutes ago, Abbas_gamer100 said: Or should I use logIn? If yes tell me how and how to connect it to the GUI. If you want to bind a player to an account, allowing you to save persistent data for every player that login in that account then the answer is yes. Simply get the text in the edit boxes, trigger a server event (remember to use client predefined variable serverside, and to not set root as source) passing the account name and the password, then use logIn to login the player. Link to comment
Abbas_gamer100 Posted January 18, 2017 Author Share Posted January 18, 2017 It shows up my password and my account name. I will try the method you posted about logIn Link to comment
TheNormalnij Posted January 18, 2017 Share Posted January 18, 2017 (edited) addEvent("submitLogin", true) local function loginHandler( username, password ) if username ~= '' and password ~= '' then local account = getAccount( username ) if account then if getAccountPlayer( account ) then -- account is currenly used triggerClientEvent( client, 'onClientLoginError', root, 2 ) else if logIn( client, account, password ) then -- on player is logged in triggerClientEvent( client, 'onClientLogin', root, username ) else -- wrong passworld triggerClientEvent( client, 'onClientLoginError', root, 3 ) end end else -- Player does not have account on the server -- Write event handler on server youself, it is easy triggerClientEvent( client, 'onClientLoginError', root, 1 ) end end end addEventHandler("submitLogin", resourceRoot, loginHandler) Write event handlers on client Edited January 18, 2017 by TheNormalnij addEvent Link to comment
Abbas_gamer100 Posted January 18, 2017 Author Share Posted January 18, 2017 Error message: Error: attempt to call getAccount(a nil value) Link to comment
TheNormalnij Posted January 18, 2017 Share Posted January 18, 2017 it is serverside onClientLoginError and onClientLogin are clientside Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now