Jump to content

Login GUI Isn't Showing and Vehicle Spawn Cmd Doesn't Work


DarkM

Recommended Posts

Alright, so I'm following the guides on the wiki and so far I've hit 2 walls. The command to spawn a vehicle doesn't work and the Login GUI doesn't show up.

Here is my script.lua:

function joinHandler()
local x = 1743
local y = -1863
local z = 14
-- spawnPlayer(source, x, y, z)
fadeCamera(source, true)
setCameraTarget(source, source)
outputChatBox("Welcome to DarkM's RP", source)
end
addEventHandler("onPlayerJoin", getRootElement(), joinHandler)
-- create the function the command handler calls, with the arguments: thePlayer, command, vehicleModel
function createVehicleForPlayer(thePlayer, command, vehicleModel)
-- create a vehicle and stuff
local x,y,z = getElementPosition(thePlayer)
x = x + 5 -- ensures vehicle wont spawn in player
-- create the vehicle and store the returned vehicle element in the ''createdVehicle'' variable
local createdVehicle = createVehicle(tonumber(vehicleModel),x,y,z)
if (createVehicle == false) then
outputChatBox("Failed to create vehicle.",thePlayer)
end
end 
-- create a command handler
addCommandHandler("createvehicle", createVehicleForPlayer)
 
-- create our loginHandler function, with username and password parameters (passed from the client gui)
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)
outputChatBox("Welcome to DarkM's RP.", client)
end
else
-- if the username or password are not correct, output message to player
outputChatBox("Invalid username and/or password. Please re-connect and try again.",client)
end
 
end
 
 
addEvent("submitLogin",true)
addEventHandler("submitLogin",root,loginHandler)

This is my gui.lua:

function createLoginWindow()
-- define the X and Y positions of the window
local X = 0.375
local Y = 0.375
-- define the width and height of the window
local Width = 0.25
local Height = 0.25
-- create the window and save its element value into the variable 'wdwLogin'
-- click on the function's name to read its documentation
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 lebel 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)
-- Max chars to 50
guiEditSetMaxLength(edtUser, 50)
guiEditSetMaxLength(edtPass, 50)
 
 X = 0.415
 Y = 0.7
 Wdith = 0.25
 Height = 0.2
 btnLogin = guiCreateButton(X, Y, Width, Height, "Log In", true, wdwLogin)
 
-- make the window invisible
guiSetVisible(wdwLogin, false)
-- now add our onClientGUIClick event to the button we just created
addEventHandler("onClientGUIClick", btnLogin, clientSubmitLogin, false)
end
 
-- attach the event handler to the root element of the resource
-- this means it will only trigger when ts own resource is started
addEventHandler("OnClientResourceStart", getResourceRootElement(getThisResource()),
function ()
-- create the log in window and its components
			createLoginWindow()
 
-- output a bried welcome message tot he player
outputChatBox("Welcome to DarkM's RP, please log in.")
 
-- if the GUI was successfully created, the show the GUI to the player
if (wdwLogin ~= nil) then
guiSetVisible(wdwLogin, true)
else
-- if the GUI hasn't been properly created, tell the player
outputChatBox("An unexpected error has occured and the log in GUI has not been created.")
end
 
-- enable the players cursor (so they can select and click on components)
showCursor(true)
-- set the input focus onto the GUI, allowing players (For example) to press 'T' without the chatbox opening
guiSetInputEnabled(true)
end
-- create the function and define the 'button' and 'state' parameters
)
-- (these are passed automatically by onClientGUIClick)
function clientSubmitLogin(button,state)
if button == "left" and state == "up" then
-- get the text entered in the 'unsername' field
local username = guiGetText(edtUser)
-- get the text entered in the 'password' field
local password = guiEditText(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 thr gui
outputChatBox("Please enter a username and password.")
 
end
end
end

Please help.

P.S. I'm using the MTA Script Editor (updated to 2.9.3)

Edited by Guest
Link to comment

It's almost 6am here and I'm tired but I'll try to help.

First, it's not OnClientResourceStart but onClientResourceStart.

I'm not really sure what is wrong with the command but I'm sure the "if" statement at line 18 will never get executed since createVehicle is a function and it will never be false unless you set it to false. You need to debug the command.

Link to comment

i'm pretty sure the souce of submitLogin must be getLocalPlayer(), not the root element:

triggerServerEvent("submitLogin", getLocalPlayer(), username, password)

and

function loginHandler(username,password)
if username == "user" and password == "apple" then
if (source) then
spawnPlayer(source, 1959.55, -1714.46, 10)
fadeCamera(source, true)
outputChatBox("Welcome to DarkM's RP.", source)
end
else
outputChatBox("Invalid username and/or password. Please re-connect and try again.", source)
end
end

Link to comment

fixed some more typos, marked with -- FIX

tested, it works. except your spawnpoint was buggy, so i made it a little higher

and you probably better restore login window if login failed, not asking player to reconnect. but thats up to you :D

client:

function createLoginWindow()
-- define the X and Y positions of the window
local X = 0.375
local Y = 0.375
-- define the width and height of the window
local Width = 0.25
local Height = 0.25
-- create the window and save its element value into the variable 'wdwLogin'
-- click on the function's name to read its documentation
 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 lebel 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)
-- Max chars to 50
guiEditSetMaxLength(edtUser, 50)
guiEditSetMaxLength(edtPass, 50)
 
 X = 0.415
 Y = 0.7
 Width = 0.25 -- FIX: Wdith > Width
 Height = 0.2
 btnLogin = guiCreateButton(X, Y, Width, Height, "Log In", true, wdwLogin)
-- make the window invisible
guiSetVisible(wdwLogin, false)
-- now add our onClientGUIClick event to the button we just created
addEventHandler("onClientGUIClick", btnLogin, clientSubmitLogin, false)
end
 
-- attach the event handler to the root element of the resource
-- this means it will only trigger when ts own resource is started
addEventHandler("onClientResourceStart", getResourceRootElement(getThisResource()),
function ()
-- create the log in window and its components
   createLoginWindow()
 
-- output a bried welcome message tot he player
outputChatBox("Welcome to DarkM's RP, please log in.")
 
-- if the GUI was successfully created, the show the GUI to the player
if (wdwLogin ~= nil) then
guiSetVisible(wdwLogin, true)
else
-- if the GUI hasn't been properly created, tell the player
outputChatBox("An unexpected error has occured and the log in GUI has not been created.")
end
 
-- enable the players cursor (so they can select and click on components)
showCursor(true)
-- set the input focus onto the GUI, allowing players (For example) to press 'T' without the chatbox opening
guiSetInputEnabled(true)
end
)
-- create the function and define the 'button' and 'state' parameters
-- (these are passed automatically by onClientGUIClick)
function clientSubmitLogin(button,state)
if button == "left" and state == "up" then
-- get the text entered in the 'unsername' field
local username = guiGetText(edtUser)
-- get the text entered in the 'password' field
local password = guiGetText(edtPass)  -- FIX: guiEditText > guiGetText
 
-- 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", getLocalPlayer(), 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 thr gui
outputChatBox("Please enter a username and password.")
end
end
end

server:

function joinHandler()
local x = 1743
local y = -1863
local z = 14
-- spawnPlayer(source, x, y, z)
-- fadeCamera(source, true)
outputChatBox("Welcome to DarkM's RP", source)
end
addEventHandler("onPlayerJoin", getRootElement(), joinHandler)
 
-- create the function the command handler calls, with the arguments: thePlayer, command, vehicleModel
function createVehicleForPlayer(thePlayer, command, vehicleModel)
-- create a vehicle and stuff
local x,y,z = getElementPosition(thePlayer)
 x = x + 5 -- ensures vehicle wont spawn in player
-- create the vehicle and store the returned vehicle element in the ''createdVehicle'' variable
local createdVehicle = createVehicle(tonumber(vehicleModel),x,y,z)
if (createdVehicle == false) then -- FIX: createVehicle > createdVehicle
outputChatBox("Failed to create vehicle.",thePlayer)
end
end
-- create a command handler
addCommandHandler("createvehicle", createVehicleForPlayer)
 
-- create our loginHandler function, with username and password parameters (passed from the client gui)
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 (source) then
spawnPlayer(source, 1959.55, -1714.46, 20)
setCameraTarget(source, source)
fadeCamera(source, true)
outputChatBox("Welcome to DarkM's RP.", source)
end
else
-- if the username or password are not correct, output message to player
outputChatBox("Invalid username and/or password. Please re-connect and try again.",source)
end
end
 
addEvent("submitLogin",true)
addEventHandler("submitLogin",root,loginHandler)

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