Jump to content

Need help making a login/register panel


Recommended Posts

I'm trying to make a login/register panel for my server and i can't get it to work the login button isn't doing anything.

function createLoginWindow()
	local X = 0.375
	local Y = 0.375
	local Width = 0.25
	local Height = 0.25
	wdwLogin = guiCreateWindow(X, Y, Width, Height, "Account Panel", true)
    guiWindowIsMovable(wdwLogin, false)
	
	-- define new X and Y positions for the first label
	X = 0.0825
	Y = 0.15
	-- 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, "First name", true, wdwLogin)
	-- alter the Y value, so the second label is slightly below the first
	Y = 0.35
	guiCreateLabel(X, Y, Width, Height, "Last name", true, wdwLogin)
    Y = 0.55
    guiCreateLabel(X, Y, Width, Height, "Password", true, wdwLogin)
	

	X = 0.415
	Y = 0.15
	Width = 0.5
	Height = 0.15
	edtFirst = guiCreateEdit(X, Y, Width, Height, "", true, wdwLogin)
	Y = 0.35
	edtLast = guiCreateEdit(X, Y, Width, Height, "", true, wdwLogin)
    Y = 0.55
	edtPass = guiCreateEdit(X, Y, Width, Height, "", true, wdwLogin)
	-- set the maximum character length for the username and password fields to 50
	guiEditSetMaxLength(edtFirst, 50)
	guiEditSetMaxLength(edtLast, 50)
    guiEditSetMaxLength(edtPass, 50)
	
    X = 0.0825
	Y = 0.8
	Width = 0.25
	Height = 0.25
	btnLogin = guiCreateButton(X, Y, Width, Height, "Log In", true, wdwLogin)
    X = 0.415
	btnRegister = guiCreateButton(X, Y, Width, Height, "Register", true, wdwLogin)
	-- make the window invisible
	guiSetVisible(wdwLogin, false)
	addEventHandler("onClientGUIClick", btnLogin, clientSubmitLogin, false)
	addEventHandler("onClientGUIClick", btnRegister, clientSubmitRegister, false)
end

addEventHandler("onClientResourceStart", getResourceRootElement(), 
	function ()
		createLoginWindow()
		guiSetVisible(wdwLogin, true)
		-- enable the player's 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 our login button was clicked with the left mouse button, and the state of the mouse button is up
	if button == "left" and state == "up" then
		local firstName = guiGetText(edtFirst)
		local lastName = guiGetText(edtLast)
		local username = firsName .. "_" .. secondName
		local password = guiGetText(edtPass)
		print(player .. username .. password)
		logIn(player, username, password)
		-- move the input focus back onto the game (allowing players to move around, open the chatbox, etc)
		guiSetInputEnabled(false)
		-- hide the window and all the components
		guiSetVisible(wdwLogin, false)
		-- hide the mouse cursor
		showCursor(false)
	end
end

Can you guys tell me what i did wrong or how can i code this panel?

Link to comment

Hello, please use <> when positing codes!

You can't use login() function on client side, you will have to add a server event and call it from client, here you have a simple example:

Client.lua

function createLoginWindow()
	local X = 0.375
	local Y = 0.375
	local Width = 0.25
	local Height = 0.25
	wdwLogin = guiCreateWindow(X, Y, Width, Height, "Account Panel", true)
    guiWindowSetMovable(wdwLogin, false)
	
	-- define new X and Y positions for the first label
	X = 0.0825
	Y = 0.15
	-- 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, "First name", true, wdwLogin)
	-- alter the Y value, so the second label is slightly below the first
	Y = 0.35
	guiCreateLabel(X, Y, Width, Height, "Last name", true, wdwLogin)
    Y = 0.55
    guiCreateLabel(X, Y, Width, Height, "Password", true, wdwLogin)
	

	X = 0.415
	Y = 0.15
	Width = 0.5
	Height = 0.15
	edtFirst = guiCreateEdit(X, Y, Width, Height, "", true, wdwLogin)
	Y = 0.35
	edtLast = guiCreateEdit(X, Y, Width, Height, "", true, wdwLogin)
    Y = 0.55
	edtPass = guiCreateEdit(X, Y, Width, Height, "", true, wdwLogin)
	-- set the maximum character length for the username and password fields to 50
	guiEditSetMaxLength(edtFirst, 50)
	guiEditSetMaxLength(edtLast, 50)
    guiEditSetMaxLength(edtPass, 50)
	
    X = 0.0825
	Y = 0.8
	Width = 0.25
	Height = 0.25
	btnLogin = guiCreateButton(X, Y, Width, Height, "Log In", true, wdwLogin)
    X = 0.415
	btnRegister = guiCreateButton(X, Y, Width, Height, "Register", true, wdwLogin)
	guiSetVisible(wdwLogin, false)
	addEventHandler("onClientGUIClick", btnLogin, clientSubmitLogin, false);
	addEventHandler("onClientGUIClick", btnRegister, clientSubmitRegister, false);
end

addEventHandler("onClientResourceStart", resourceRoot, function()
	createLoginWindow();
	guiSetVisible(wdwLogin, true);
	showCursor(true);
    guiSetInputEnabled(true);
end);

function clientSubmitLogin(button, state)
	if button == "left" and state == "up" then
		local firstName = guiGetText(edtFirst);
		local lastName = guiGetText(edtLast);
		local username = firsName .. "_" .. secondName;
		local password = guiGetText(edtPass);
		--print(player .. username .. password);
		if username and password then
			triggerServerEvent("customLoginPlayer", localPlayer, username, password);
		end
		guiSetInputEnabled(false);
		guiSetVisible(wdwLogin, false);
		showCursor(false);
	end
end

function clientSubmitRegister(button, state)
	if button == "left" and state == "up" then
		local firstName = guiGetText(edtFirst);
		local lastName = guiGetText(edtLast);
		local username = firsName .. "_" .. secondName;
		local password = guiGetText(edtPass);
		--print(player .. username .. password);
		if username and password then
			triggerServerEvent("customLoginPlayer", localPlayer, username, password);
		end
		guiSetInputEnabled(false);
		guiSetVisible(wdwLogin, false);
		showCursor(false);
	end
end

Server.lua

addEvent("customLoginPlayer", true);
addEventHandler("customLoginPlayer", root, function(username, password)
	local account = getAccount(username, password);
	if not account then
		outputChatBox("No account found, please check credentials!");
	else
		logIn(client, account, password);
		outputChatBox("You got logged in, woha!");
	end
end);


addEvent("customRegisterPlayer", true);
addEventHandler("customRegisterPlayer", root, function(username, password)
	if not getAccount(username) then
		local theAccount = addAccount(username, password);
		if theAccount then
			logIn(client, theAccount, password);
			outputChatBox("You got logged in, woha!");
		end
	else
		outputChatBox("Oupss, there is already an account with this username!");
	end
end);

Keep in mind this resource have to have admin rights!

Hope that help you out! :)

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