Jump to content

Help! I am new to scripting


Recommended Posts

so i am using mta wiki to learn scripting , recently i tried to script login window from the "Introduction to Scripting the GUI" tutorial but it is not working idk why 

gui.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, "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)
		guiSetVisible(wdwLogin, false)
end

addEventHandler("onClientResourceStart",getResourceRootElement(),
        function ()
		        createLoginWindow()
				
				outputChatBox("Welcome to My MTA:SA Server, please log in.")
				if (wdwLogin ~= nil) then
				        guiSetVisible(wdwlogin, true)
				else
						outputChatBox("An unexpected error has occurred.")
				end
				showCursor(true)
				guiSetInputEnabled(true)
	    end
)
function createLoginWindow()
        addEventHandler("onClientGUIClick",btnLogin, clientSubmitLogin, false)
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)
		        guiSetInputEnabled(false)
				guiSetVisible(wdwLogin, false)
				showCursor(false)
			else
				outputChatBox("Please enter a username and password.")
			end
		end
end

function loginHandler(username,password)
if username =="user" and password =="apple" then
		if (client) then
				spawnPlayer(client, 1959.55, -1714.46 , 16)
				fadeCamera(client, true)
				setCameraTarget(client, client)
				outputChatBox("Welcome to My Server.", client)
		end
	else
		outputChatBox("invalid username and password. Please re-connect and try again.",client)
	end
end
addEvent("submitLogin",true)
addEventHandler("submitLogin",root,loginHandler)

meta.xml

<meta>
<info author="Ghost_447" version="1.0.0" name="LoginPanel" />
<script src="client/gui.Lua" type="client" />
</meta>

can someone help me and tell me what is the problem with it

thanks :D

Edited by Ghost_447
Link to comment
function loginHandler(username,password)
if username =="user" and password =="apple" then
		if (client) then
				spawnPlayer(client, 1959.55, -1714.46 , 16)
				fadeCamera(client, true)
				setCameraTarget(client, client)
				outputChatBox("Welcome to My Server.", client)
		end
	else
		outputChatBox("invalid username and password. Please re-connect and try again.",client)
	end
end
addEvent("submitLogin",true)
addEventHandler("submitLogin",root,loginHandler)

 

That code should be in server side.

Link to comment

@HassoN

7 minutes ago, HassoN said:

set guiSetVisible to true  at line 29

it is set to false in tutorial because we need to show the window when we need not always

 

guiSetVisible(wdwLogin, false) --hides all the GUI we made so we can show them to the player at the appropriate moment. 

edit

i did what you said still the same result 

Edited by Ghost_447
Link to comment
1 minute ago, Ghost_447 said:

@HassoN

it is set to false in tutorial because we need to show the window when we need not always

 


guiSetVisible(wdwLogin, false) --hides all the GUI we made so we can show them to the player at the appropriate moment. 

 

That's true but you are calling it once the resource starts so it won't make any difference... for example you could do something like:

 

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)
		guiSetVisible(wdwLogin, true)
		showCursor(true)
		guiSetInputEnabled(true)
		outputChatBox("Welcome to My MTA:SA Server, please log in.")
end
addEventHandler("onClientResourceStart", resourceRoot, createLoginWindow)

Instead of your 2 functions code:

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)
		guiSetVisible(wdwLogin, false)
end

addEventHandler("onClientResourceStart",getResourceRootElement(),
        function ()
		        createLoginWindow()
				
				outputChatBox("Welcome to My MTA:SA Server, please log in.")
				if (wdwLogin ~= nil) then
				        guiSetVisible(wdwlogin, true)
				else
						outputChatBox("An unexpected error has occurred.")
				end
				showCursor(true)
				guiSetInputEnabled(true)
	    end
)

 

Filling your script with duplicated functions is not always a good idea.

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