Jump to content

not getting the player in a function


Quebec

Recommended Posts

Hey, I'm trying to make a very basic login system just for learning and I'm trying to pass the player to the function but i don't know how. Error I get is:  line 25 "Bad argument @ setElementData: [Expected element as argument 1, got nil]". Here's the code:

addEvent('accountsystem:login', true)
addEventHandler('accountsystem:login', root, function(thePlayer, username, password)
	if not username or not password then return false end
	local queryCheck = dbQuery(db, "SELECT * FROM `accounts` WHERE `username` = '" .. username .. "'")
	local result = dbPoll(queryCheck, -1)
	if result then
		for k, row in ipairs(result)do
			local hashedPassword = row["password"]
			if passwordVerify(password, hashedPassword) then
				outputChatBox("Ai fost autentificat cu succes!", thePlayer, 100, 255, 100)
				setElementData(thePlayer, "account:id", row["id"])
				setElementData(thePlayer, "account:ip", row["ip"])
				setElementData(thePlayer, "account:username", row["username"])
				spawnPlayer(thePlayer, 0, 5, 5)
				triggerClientEvent(thePlayer, 'accountsystem:schimbacamera', thePlayer)
			else
				outputChatBox("Contul/Parola sunt incorecte!", thePlayer, 255, 100, 100)
			end
		end
	end
end)

 

Edited by Quebec
Link to comment
8 minutes ago, The_GTA said:

Please post the full code, especially the part where you trigger the event on the clientside.

Client side script:
 


addEventHandler('onClientResourceStart', root, function()
	setCameraMatrix (0, 0, 100, 0, 100, 50)
	fadeCamera(true)
end)

function validateRegisterData(command, username, password)
	if username == "" or password == "" then
		return outputChatBox("SINTAXA:", 255, 100, 100)
	end

	if string.len(username) < 3 then
		return outputChatBox("Numele trebuie sa fie mai mare de 3 caractere!", 255, 100, 100)
	elseif string.len(password) < 6 then
		return outputChatBox("Parola trebuie sa fie mai mare de 6 caractere!", 255, 100, 100)
	end
	triggerServerEvent('accountsystem:register', localPlayer, localPlayer, username, password)
end
addCommandHandler('registeraccount', validateRegisterData)

function validateLoginData(command, username, password)
	if username == "" or password == "" then
		return outputChatBox("Umple toate spatiile!", 255, 100, 100)
	end
	triggerServerEvent('accountsystem:login', localPlayer, loginPlayer, username, password)
end
addCommandHandler('loginaccount', validateLoginData)

addEvent('accountsystem:schimbacamera', true)
addEventHandler('accountsystem:schimbacamera', root, function()
	setCameraTarget(localPlayer, localPlayer)
end)

 

Server side script:

local db = exports.mysqlconnection:getConnection()

addEvent('accountsystem:register', true)
addEventHandler('accountsystem:register', root, function(thePlayer, username, password)
	if not username or not password then return false end
	local queryCheck = dbQuery(db, "SELECT * FROM `accounts` WHERE `username` = '" .. username .. "'")
	local result = dbPoll(queryCheck, -1)
	if #result > 0 then return outputChatBox("Acest nume este deja folosit!", thePlayer) end
	local playerIP = getPlayerIP(thePlayer)
	local hash = passwordHash(password, "bcrypt", {})
	dbExec(db, "INSERT INTO `accounts` (`username`, `password`, `ip`) VALUES (?, ?, ?)", username, hash, playerIP)
	outputChatBox("Contul a fost creat cu succes!", thePlayer, 100, 255, 100)
end)

addEvent('accountsystem:login', true)
addEventHandler('accountsystem:login', root, function(thePlayer, username, password)
	if not username or not password then return false end
	local queryCheck = dbQuery(db, "SELECT * FROM `accounts` WHERE `username` = '" .. username .. "'")
	local result = dbPoll(queryCheck, -1)
	if result then
		for k, row in ipairs(result) do
			local hashedPassword = row["password"]
			setElementData(thePlayer, "account:id", row["id"])
			setElementData(thePlayer, "account:ip", row["ip"])
			setElementData(thePlayer, "account:username", row["username"])
			if passwordVerify(password, hashedPassword) then
				outputChatBox("Ai fost autentificat cu succes!", thePlayer, 100, 255, 100)
				spawnPlayer(thePlayer, 0, 5, 5)
				triggerClientEvent(thePlayer, 'accountsystem:schimbacamera', thePlayer)
			else
				outputChatBox("Contul/Parola sunt incorecte!", thePlayer, 255, 100, 100)
			end
		end
	else
		return outputChatBox("Acest cont nu exista!", thePlayer, 255, 100, 100)
	end
end)

 

Link to comment

To answer your original question, please take a look at this code:

function validateLoginData(command, username, password)
	if username == "" or password == "" then
		return outputChatBox("Umple toate spatiile!", 255, 100, 100)
	end
	triggerServerEvent('accountsystem:login', localPlayer, loginPlayer, username, password)
end
addCommandHandler('loginaccount', validateLoginData)

The third argument to triggerServerEvent, the global variable "loginPlayer", is not defined anywhere. Did you mean to use "localPlayer"?

  • Thanks 1
Link to comment
10 minutes ago, The_GTA said:

To answer your original question, please take a look at this code:

function validateLoginData(command, username, password)
	if username == "" or password == "" then
		return outputChatBox("Umple toate spatiile!", 255, 100, 100)
	end
	triggerServerEvent('accountsystem:login', localPlayer, loginPlayer, username, password)
end
addCommandHandler('loginaccount', validateLoginData)

The third argument to triggerServerEvent, the global variable "loginPlayer", is not defined anywhere. Did you mean to use "localPlayer"?

Didn't even notice that, it's a typo. Lemme check if this works.

12 minutes ago, The_GTA said:

To answer your original question, please take a look at this code:

function validateLoginData(command, username, password)
	if username == "" or password == "" then
		return outputChatBox("Umple toate spatiile!", 255, 100, 100)
	end
	triggerServerEvent('accountsystem:login', localPlayer, loginPlayer, username, password)
end
addCommandHandler('loginaccount', validateLoginData)

The third argument to triggerServerEvent, the global variable "loginPlayer", is not defined anywhere. Did you mean to use "localPlayer"?

Works like a charm, thanks a lot! :D

  • Like 1
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...