Jump to content

[Help] DB in DGS


Recommended Posts

Hello Forum!
I need yor help 😄

I am just starting to learn programming in Lua, there are not many guides, so I turn to you.

I wrote my own passport emulation, but I don't understand how I can put the entered data into the database. Below I will attach my code.

Also, I would be happy if you could help me and tell me how I can add the display of this "passport" when entering a marker using addEventHendler.

Thank you in advance!

My code 

local dgs = exports.dgs
local sw,sh = guiGetScreenSize() -- разрешение экрана игрока
local px,py = sw/1920,sh/1080 -- адаптация экрана


local window = dgs:dgsCreateWindow(((sw-800)/2)*px,((sh-600)/2)*py,800*px,600*py,"Паспорт",false)
local buttonExecute = dgs:dgsCreateButton(325*px,530*py,150*px,40*py,"Подтвердить",false,window,nil,nil,nil,nil,nil,nil,tocolor(255,0,0),tocolor(100,0,0),tocolor(255,0,0))

local labelFirstName = dgs:dgsCreateLabel(200*px,200*py,400*px,30*py," Имя: ",false,window)
local editFirstName = dgs:dgsCreateEdit(200*px,220*py,400*px,30*py,"",false,window,tocolor(255,255,255),nil,nil,nil)
dgs:dgsSetProperty(labelFirstName,"alignment",{"center"}, {"center"})
dgs:dgsSetProperty(editFirstName,"alignment",{"center"}, {"center"})

local labelLastName = dgs:dgsCreateLabel(200*px,270*py,400*px,30*py," Фамилия: ",false,window)
local editLastName = dgs:dgsCreateEdit(200*px,290*py,400*px,30*py,"",false,window,tocolor(255,255,255),nil,nil,nil)
dgs:dgsSetProperty(labelLastName,"alignment",{"center"}, {"center"})
dgs:dgsSetProperty(editLastName,"alignment",{"center"}, {"center"})

local labelAge = dgs:dgsCreateLabel(200*px,340*py,400*px,30*py," Возраст: ",false,window,tocolor(255,255,255),nil,nil,nil)
local editAge = dgs:dgsCreateEdit(200*px,360*py,400*px,30*py,"",false,window)
dgs:dgsSetProperty(labelAge,"alignment",{"center"}, {"center"})
dgs:dgsSetProperty(editAge,"alignment",{"center"}, {"center"})

local labelCountry = dgs:dgsCreateLabel(200*px,410*py,400*px,30*py," Страна: ",false,window,tocolor(255,255,255),nil,nil,nil,tocolor(100,100,100,100))
local editCountry = dgs:dgsCreateEdit(200*px,430*py,400*px,30*py,"",false,window)
dgs:dgsSetProperty(labelCountry,"alignment",{"center"}, {"center"})
dgs:dgsSetProperty(editCountry,"alignment",{"center"}, {"center"})


dgs:dgsWindowSetMovable(window,false)
dgs:dgsWindowSetSizable(window,false)
dgs:dgsWindowSetCloseButtonEnabled(window,false)
dgs:dgsSetVisible(window,true)

dgs:dgsSetVisible(window,true)
showCursor(true)

function openPanel()
	dgs:dgsSetVisible(window,true)
	showCursor(true)
end

function colsePanel()
	dgs:dgsSetVisible(window,false)
	showCursor(false)
end

local currentChose = "register"

addEventHandler("onDgsMouseClick",root,function(btn,state)
	if btn == "left" and state == "down" then
		if source == buttonExecute then
			local firstName = dgs:dgsGetText(editFirstName)
			local lastName = dgs:dgsGetText(editLastName)
			local age = dgs:dgsGetText(editAge)
			local country = dgs:dgsGetText(editCountry)
			if not string.find(firstName,"%S") then outputChatBox("Name") return end
			if not string.find(lastName,"%S") then outputChatBox("LastName") return end
			if not string.find(age,"%d") then outputChatBox("Age") return end
			if not string.find(country,"%S") then outputChatBox("Country") return end
			triggerServerEvent("playerPassportEnter",localPlayer,firstName,lastName,age,country)
		end
	end
end)

If you do help me, please write this with explanations, thank you in advance! I LOVE YOU FORUM ❤️

  • Like 1
Link to comment
5 hours ago, nikitau said:

Hello Forum!
I need yor help 😄

I am just starting to learn programming in Lua, there are not many guides, so I turn to you.

I wrote my own passport emulation, but I don't understand how I can put the entered data into the database. Below I will attach my code.

Also, I would be happy if you could help me and tell me how I can add the display of this "passport" when entering a marker using addEventHendler.

Thank you in advance!

My code 

local dgs = exports.dgs
local sw,sh = guiGetScreenSize() -- разрешение экрана игрока
local px,py = sw/1920,sh/1080 -- адаптация экрана


local window = dgs:dgsCreateWindow(((sw-800)/2)*px,((sh-600)/2)*py,800*px,600*py,"Паспорт",false)
local buttonExecute = dgs:dgsCreateButton(325*px,530*py,150*px,40*py,"Подтвердить",false,window,nil,nil,nil,nil,nil,nil,tocolor(255,0,0),tocolor(100,0,0),tocolor(255,0,0))

local labelFirstName = dgs:dgsCreateLabel(200*px,200*py,400*px,30*py," Имя: ",false,window)
local editFirstName = dgs:dgsCreateEdit(200*px,220*py,400*px,30*py,"",false,window,tocolor(255,255,255),nil,nil,nil)
dgs:dgsSetProperty(labelFirstName,"alignment",{"center"}, {"center"})
dgs:dgsSetProperty(editFirstName,"alignment",{"center"}, {"center"})

local labelLastName = dgs:dgsCreateLabel(200*px,270*py,400*px,30*py," Фамилия: ",false,window)
local editLastName = dgs:dgsCreateEdit(200*px,290*py,400*px,30*py,"",false,window,tocolor(255,255,255),nil,nil,nil)
dgs:dgsSetProperty(labelLastName,"alignment",{"center"}, {"center"})
dgs:dgsSetProperty(editLastName,"alignment",{"center"}, {"center"})

local labelAge = dgs:dgsCreateLabel(200*px,340*py,400*px,30*py," Возраст: ",false,window,tocolor(255,255,255),nil,nil,nil)
local editAge = dgs:dgsCreateEdit(200*px,360*py,400*px,30*py,"",false,window)
dgs:dgsSetProperty(labelAge,"alignment",{"center"}, {"center"})
dgs:dgsSetProperty(editAge,"alignment",{"center"}, {"center"})

local labelCountry = dgs:dgsCreateLabel(200*px,410*py,400*px,30*py," Страна: ",false,window,tocolor(255,255,255),nil,nil,nil,tocolor(100,100,100,100))
local editCountry = dgs:dgsCreateEdit(200*px,430*py,400*px,30*py,"",false,window)
dgs:dgsSetProperty(labelCountry,"alignment",{"center"}, {"center"})
dgs:dgsSetProperty(editCountry,"alignment",{"center"}, {"center"})


dgs:dgsWindowSetMovable(window,false)
dgs:dgsWindowSetSizable(window,false)
dgs:dgsWindowSetCloseButtonEnabled(window,false)
dgs:dgsSetVisible(window,true)

dgs:dgsSetVisible(window,true)
showCursor(true)

function openPanel()
	dgs:dgsSetVisible(window,true)
	showCursor(true)
end

function colsePanel()
	dgs:dgsSetVisible(window,false)
	showCursor(false)
end

local currentChose = "register"

addEventHandler("onDgsMouseClick",root,function(btn,state)
	if btn == "left" and state == "down" then
		if source == buttonExecute then
			local firstName = dgs:dgsGetText(editFirstName)
			local lastName = dgs:dgsGetText(editLastName)
			local age = dgs:dgsGetText(editAge)
			local country = dgs:dgsGetText(editCountry)
			if not string.find(firstName,"%S") then outputChatBox("Name") return end
			if not string.find(lastName,"%S") then outputChatBox("LastName") return end
			if not string.find(age,"%d") then outputChatBox("Age") return end
			if not string.find(country,"%S") then outputChatBox("Country") return end
			triggerServerEvent("playerPassportEnter",localPlayer,firstName,lastName,age,country)
		end
	end
end)

If you do help me, please write this with explanations, thank you in advance! I LOVE YOU FORUM ❤️

Server side you have to add this - >

local connect = dbConnect("sqlite","Passport.db") -- we create the db 
-- then we create a table 
dbExec(connect,"CREATE TABLE IF NOT EXISTS passport (id INTEGER PRIMARY KEY AUTOINCREMENT,accountName TEXT,firstname TEXT,lastname TEXT,age INTEGER,country TEXT)")

-- we add the server side event 
addEvent("playerPassportEnter",true)
addEventHandler("playerPassportEnter",getRootElement(),function(firstname,lastname,age,country)
    if client then  -- check if data is from client you use always client when you bring data from clinet
        local acc_name = getAccountName(getPlayerAccount(client)) -- get the player account name
        local query = dbQuery(connect,"SELECT * FROM passport WHERE firstname=? OR lastname=? OR accountName=?",tostring(firstname),tostring(lastname),acc_name)
        local rezult = dbPoll(query,-1) -- we select the data and then get the rezult

        if #rezult > 0 then -- we check if the rezult table has some data with this player account
            outputChatBox("[PASSPORT] This firstname or lastname is used or you already created a passport!",client,104,255,104)
        else  -- if we dont have any data with this player we add a passport!
            dbExec(connect,"INSERT INTO passport (accountName,firstname,lastname,age,country) VALUES(?,?,?,?,?)",acc_name,firstname,lastname,age,country)
            outputChatBox("[PASSPORT] New passport has been created successfully!",client,104,255,104)
        end
    end 
end)

-- you put your x,y,z coordinates where your marker is or where you want to create the marker --
local viewPassport = createMarker(2865.15063,-1989.56226,11.10156-1.7,"cylinder",2.0,104,255,104,104) 

addEventHandler('onMarkerHit',getResourceRootElement(),function(hitElement,matchingDimension)
    if getElementType(hitElement) == 'player' then -- check if the element which hits the marker is a player
        if source == viewPassport then -- we check if we hit our marker and not another marker 
            local acc_name = getAccountName(getPlayerAccount(hitElement))
            local query = dbQuery(connect,"SELECT * FROM passport WHERE accountName=?",acc_name) -- we select the data from db
            local rezult = dbPoll(query,-1)

            if #rezult > 0 then  -- if we have data we sent it to client 
                triggerClientEvent("viewPassport",hitElement,rezult)
            else  -- if we do not have data we write a message to player
                outputChatBox("[PASSPORT] You dont have a passport, to see a passport you need to create one first!",hitElement,104,255,104) 
            end 
        end 
    end 
end)

Client Side we have to add this ->

local on = 0 
addEvent("viewPassport",true)
addEventHandler("viewPassport",getRootElement(),function(rezult)
    if on == 1 then return outputChatBox("[PASSPORT] Your passport window is open already!",104,250,104) end 
    on = 1
    window = guiCreateWindow((sw-400)/2,(sh-400)/2,300,200,"Passport Data")
    guiWindowSetMovable(window,false)
    guiWindowSetSizable(window,false)

    passport_info = guiCreateMemo(10,20,380,275,"FirstName: "..rezult[1]['firstname'].."\nLastName: "..rezult[1]['lastname'].."\nAge: "..rezult[1]['age'].."\nCountry: "..rezult[1]['country'].."",false,window)
    guiMemoSetReadOnly(passport_info,true)

    close_passport = guiCreateButton(10,170,380,125,"CLOSE",false,window)
    showCursor(true) 

    addEventHandler("onClientGUIClick",close_passport,close_pass,false)
end)

close_pass = function(button,state)
    if (button == "left") and (state == "up") then 
        showCursor(false)
        guiSetVisible(window,false)
    	on = 0
    end 
end 

Sure on client side you can create with dgs what i made like the window,button and the memo the way you want. I used the normal gui because i am used to it and i dont use any library.

Also a link to sqlite browser to check if your table is created and data is inserted into the table.

https://sqlitebrowser.org/dl/

Edited by FlorinSzasz
Link to comment
14 hours ago, FlorinSzasz said:

Server side you have to add this - >

local connect = dbConnect("sqlite","Passport.db") -- we create the db 
-- then we create a table 
dbExec(connect,"CREATE TABLE IF NOT EXISTS passport (id INTEGER PRIMARY KEY AUTOINCREMENT,accountName TEXT,firstname TEXT,lastname TEXT,age INTEGER,country TEXT)")

-- we add the server side event 
addEvent("playerPassportEnter",true)
addEventHandler("playerPassportEnter",getRootElement(),function(firstname,lastname,age,country)
    if client then  -- check if data is from client you use always client when you bring data from clinet
        local acc_name = getAccountName(getPlayerAccount(client)) -- get the player account name
        local query = dbQuery(connect,"SELECT * FROM passport WHERE firstname=? OR lastname=? OR accountName=?",tostring(firstname),tostring(lastname),acc_name)
        local rezult = dbPoll(query,-1) -- we select the data and then get the rezult

        if #rezult > 0 then -- we check if the rezult table has some data with this player account
            outputChatBox("[PASSPORT] This firstname or lastname is used or you already created a passport!",client,104,255,104)
        else  -- if we dont have any data with this player we add a passport!
            dbExec(connect,"INSERT INTO passport (accountName,firstname,lastname,age,country) VALUES(?,?,?,?,?)",acc_name,firstname,lastname,age,country)
            outputChatBox("[PASSPORT] New passport has been created successfully!",client,104,255,104)
        end
    end 
end)

-- you put your x,y,z coordinates where your marker is or where you want to create the marker --
local viewPassport = createMarker(2865.15063,-1989.56226,11.10156-1.7,"cylinder",2.0,104,255,104,104) 

addEventHandler('onMarkerHit',getResourceRootElement(),function(hitElement,matchingDimension)
    if getElementType(hitElement) == 'player' then -- check if the element which hits the marker is a player
        if source == viewPassport then -- we check if we hit our marker and not another marker 
            local acc_name = getAccountName(getPlayerAccount(hitElement))
            local query = dbQuery(connect,"SELECT * FROM passport WHERE accountName=?",acc_name) -- we select the data from db
            local rezult = dbPoll(query,-1)

            if #rezult > 0 then  -- if we have data we sent it to client 
                triggerClientEvent("viewPassport",hitElement,rezult)
            else  -- if we do not have data we write a message to player
                outputChatBox("[PASSPORT] You dont have a passport, to see a passport you need to create one first!",hitElement,104,255,104) 
            end 
        end 
    end 
end)

Client Side we have to add this ->

local on = 0 
addEvent("viewPassport",true)
addEventHandler("viewPassport",getRootElement(),function(rezult)
    if on == 1 then return outputChatBox("[PASSPORT] Your passport window is open already!",104,250,104) end 
    on = 1
    window = guiCreateWindow((sw-400)/2,(sh-400)/2,300,200,"Passport Data")
    guiWindowSetMovable(window,false)
    guiWindowSetSizable(window,false)

    passport_info = guiCreateMemo(10,20,380,275,"FirstName: "..rezult[1]['firstname'].."\nLastName: "..rezult[1]['lastname'].."\nAge: "..rezult[1]['age'].."\nCountry: "..rezult[1]['country'].."",false,window)
    guiMemoSetReadOnly(passport_info,true)

    close_passport = guiCreateButton(10,170,380,125,"CLOSE",false,window)
    showCursor(true) 

    addEventHandler("onClientGUIClick",close_passport,close_pass,false)
end)

close_pass = function(button,state)
    if (button == "left") and (state == "up") then 
        showCursor(false)
        guiSetVisible(window,false)
    	on = 0
    end 
end 

Sure on client side you can create with dgs what i made like the window,button and the memo the way you want. I used the normal gui because i am used to it and i dont use any library.

Also a link to sqlite browser to check if your table is created and data is inserted into the table.

https://sqlitebrowser.org/dl/

Thank's man!
Two more question

1. How can I retrieve and display data from the database without the ability to edit it?
2. How can I close this panel with the "Close" button?
I added your server-side part and based on your client-side, I wrote my own library for DGS.

Client
local dgs = exports.dgs
local sw,sh = guiGetScreenSize() -- разрешение экрана игрока
local px,py = sw/1920,sh/1080 -- адаптация экрана


local window = dgs:dgsCreateWindow(((sw-800)/2)*px,((sh-600)/2)*py,800*px,600*py,"Паспорт",false)
local buttonExecute = dgs:dgsCreateButton(325*px,530*py,150*px,40*py,"Подтвердить",false,window,nil,nil,nil,nil,nil,nil,tocolor(255,0,0),tocolor(100,0,0),tocolor(255,0,0))

local labelFirstName = dgs:dgsCreateLabel(200*px,200*py,400*px,30*py," Имя: ",false,window)
local editFirstName = dgs:dgsCreateEdit(200*px,220*py,400*px,30*py,"",false,window,tocolor(255,255,255),nil,nil,nil)
dgs:dgsSetProperty(labelFirstName,"alignment",{"center"}, {"center"})
dgs:dgsSetProperty(editFirstName,"alignment",{"center"}, {"center"})

local labelLastName = dgs:dgsCreateLabel(200*px,270*py,400*px,30*py," Фамилия: ",false,window)
local editLastName = dgs:dgsCreateEdit(200*px,290*py,400*px,30*py,"",false,window,tocolor(255,255,255),nil,nil,nil)
dgs:dgsSetProperty(labelLastName,"alignment",{"center"}, {"center"})
dgs:dgsSetProperty(editLastName,"alignment",{"center"}, {"center"})

local labelAge = dgs:dgsCreateLabel(200*px,340*py,400*px,30*py," Возраст: ",false,window,tocolor(255,255,255),nil,nil,nil)
local editAge = dgs:dgsCreateEdit(200*px,360*py,400*px,30*py,"",false,window)
dgs:dgsSetProperty(labelAge,"alignment",{"center"}, {"center"})
dgs:dgsSetProperty(editAge,"alignment",{"center"}, {"center"})

local labelCountry = dgs:dgsCreateLabel(200*px,410*py,400*px,30*py," Страна: ",false,window,tocolor(255,255,255),nil,nil,nil,tocolor(100,100,100,100))
local editCountry = dgs:dgsCreateEdit(200*px,430*py,400*px,30*py,"",false,window)
dgs:dgsSetProperty(labelCountry,"alignment",{"center"}, {"center"})
dgs:dgsSetProperty(editCountry,"alignment",{"center"}, {"center"})


dgs:dgsWindowSetMovable(window,false)
dgs:dgsWindowSetSizable(window,false)
dgs:dgsWindowSetCloseButtonEnabled(window,false)
dgs:dgsSetVisible(window,true)

dgs:dgsSetVisible(window,false)
showCursor(false)

function openPanel()
	dgs:dgsSetVisible(window,true)
	showCursor(true)
end

function colsePanel()
	dgs:dgsSetVisible(window,false)
	showCursor(false)
end

local currentChose = "register"

addEventHandler("onDgsMouseClick",root,function(btn,state)
	if btn == "left" and state == "down" then
		if source == buttonExecute then
			local firstName = dgs:dgsGetText(editFirstName)
			local lastName = dgs:dgsGetText(editLastName)
			local age = dgs:dgsGetText(editAge)
			local country = dgs:dgsGetText(editCountry)
			if not string.find(firstName,"%S") then outputChatBox("Name") return end
			if not string.find(lastName,"%S") then outputChatBox("LastName") return end
			if not string.find(age,"%d") then outputChatBox("Age") return end
			if not string.find(country,"%S") then outputChatBox("Country") return end
			triggerServerEvent("playerPassportEnter",localPlayer,firstName,lastName,age,country)
		end
	end
end)

local on = 0 
addEvent("viewPassport",true)
addEventHandler("viewPassport",getRootElement(),function(rezult)
    if on == 1 then return outputChatBox("[PASSPORT] Your passport window is open already!",104,250,104) end 
    
    on = 1
    local passport_info = dgs:dgsCreateWindow(((sw-800)/2)*px,((sh-600)/2)*py,800*px,600*py,"Паспорт",false)
    local closeButton = dgs:dgsCreateButton(325*px,530*py,150*px,40*py,"Close",false,passport_info,nil,nil,nil,nil,nil,nil,tocolor(255,0,0),tocolor(100,0,0),tocolor(255,0,0))
    local firstNameInfo = dgs:dgsCreateMemo(10,20,150,25,"Имя: "..rezult[1]['FirstName'].."",false,passport_info)
    local lirstNameInfo = dgs:dgsCreateMemo(10,40,150,25,"Фамилия: "..rezult[1]['LastName'].."",false,passport_info)
    local ageInfo = dgs:dgsCreateMemo(10,60,150,25,"Возраст: "..rezult[1]['Age'].."",false,passport_info)
    local CountryInfo = dgs:dgsCreateMemo(10,100,150,25,"Страна: "..rezult[1]['Country'].."",false,passport_info)

    showCursor(true) 
	dgs:dgsWindowSetMovable(passport_info,false)
	dgs:dgsWindowSetSizable(passport_info,false)
end)

 

Server

 

local connect = dbConnect("sqlite","db.db") -- we create the db 
-- then we create a table 
-- we add the server side event 
addEvent("playerPassportEnter",true)
addEventHandler("playerPassportEnter",getRootElement(),function(firstname,lastname,age,country)
    if client then  -- check if data is from client you use always client when you bring data from clinet
        local acc_name = getAccountName(getPlayerAccount(client)) -- get the player account name
        local query = dbQuery(connect,"SELECT * FROM Users WHERE firstname=? OR lastname=? OR accountName=?",tostring(firstname),tostring(lastname),acc_name)
        local rezult = dbPoll(query,-1) -- we select the data and then get the rezult

        if #rezult > 0 then -- we check if the rezult table has some data with this player account
            outputChatBox("[PASSPORT] This firstname or lastname is used or you already created a passport!",client,104,255,104)
        else  -- if we dont have any data with this player we add a passport!
            dbExec(connect,"INSERT INTO Users (accountName,firstname,lastname,age,country) VALUES(?,?,?,?,?)",acc_name,firstname,lastname,age,country)
            outputChatBox("[PASSPORT] New passport has been created successfully!",client,104,255,104)
        end
    end 
end)

-- you put your x,y,z coordinates where your marker is or where you want to create the marker --
local viewPassport = createMarker(-713.0, 955.0, 12.3-1.7,"cylinder",2.0,104,255,104,104) 

addEventHandler('onMarkerHit',getResourceRootElement(),function(hitElement,matchingDimension)
    if getElementType(hitElement) == 'player' then -- check if the element which hits the marker is a player
        if source == viewPassport then -- we check if we hit our marker and not another marker 
            local acc_name = getAccountName(getPlayerAccount(hitElement))
            local query = dbQuery(connect,"SELECT * FROM Users WHERE accountName=?",acc_name) -- we select the data from db
            local #rezult = dbPoll(query,-1)

            if #rezult > 0 then  -- if we have data we sent it to client 
                triggerClientEvent("viewPassport",hitElement,rezult)
            else  -- if we do not have data we write a message to player
                outputChatBox("[PASSPORT] You dont have a passport, to see a passport you need to create one first!",hitElement,104,255,104) 
            end 
        end 
    end 
end)


My DB
 

CREATE TABLE "Users" (
	"ID"	INTEGER NOT NULL UNIQUE,
	"accountName"	TEXT,
	"FirstName"	TEXT NOT NULL,
	"LastName"	TEXT NOT NULL,
	"Age"	TEXT NOT NULL,
	"Country"	TEXT NOT NULL,
	PRIMARY KEY("ID" AUTOINCREMENT)
);

 

  • Like 1
Link to comment

1. To close your panel with Close button you have to use the dgs event like you did before.

2. You cant edit the data from db without a db browser or an update statement.

3.You get the data and send it to client on the marker hit event.

4. If you want to get the data from db on another function or event you can but you need to have the player who triggers that event.

-- client side
--a)
--remove ,,local,, before the button 
local closeButton = dgs:dgsCreateButton(325*px,530*py,150*px,40*py,"Close",false,passport_info,nil,nil,nil,nil,nil,nil,tocolor(255,0,0),tocolor(100,0,0),tocolor(255,0,0))
-- you should have this -- 
closeButton = dgs:dgsCreateButton(325*px,530*py,150*px,40*py,"Close",false,passport_info,nil,nil,nil,nil,nil,nil,tocolor(255,0,0),tocolor(100,0,0),tocolor(255,0,0))

-- b) 
addEventHandler("onDgsMouseClick",root,function(btn,state)
	if btn == "left" and state == "down" then
		if source == closeButton then
		showCursor(false) 
        	dgs:dgsSetVisible(passport_info,false)
        	on = 0
		end
	end
end)

-- server side 
-- if you changed the column names in your db you have to replace them in the query and insert statement 
-- old 
local query = dbQuery(connect,"SELECT * FROM Users WHERE firstname=? OR lastname=? OR accountName=?",tostring(firstname),tostring(lastname),acc_name)
-- new 
local query = dbQuery(connect,"SELECT * FROM Users WHERE FirstName=? OR LastName=? OR accountName=?",tostring(firstname),tostring(lastname),acc_name)

--- also this 
-- old 
dbExec(connect,"INSERT INTO Users (accountName,firstname,lastname,age,country) VALUES(?,?,?,?,?)",acc_name,firstname,lastname,age,country)
-- new 
dbExec(connect,"INSERT INTO Users (accountName,FirstName,LastName,Age,Country) VALUES(?,?,?,?,?)",acc_name,firstname,lastname,age,country)
Edited by FlorinSzasz
Link to comment
43 minutes ago, FlorinSzasz said:

1. To close your panel with Close button you have to use the dgs event like you did before.

2. You cant edit the data from db without a db browser or an update statement.

3.You get the data and send it to client on the marker hit event.

4. If you want to get the data from db on another function or event you can but you need to have the player who triggers that event.

-- client side
--a)
--remove ,,local,, before the button 
local closeButton = dgs:dgsCreateButton(325*px,530*py,150*px,40*py,"Close",false,passport_info,nil,nil,nil,nil,nil,nil,tocolor(255,0,0),tocolor(100,0,0),tocolor(255,0,0))
-- you should have this -- 
closeButton = dgs:dgsCreateButton(325*px,530*py,150*px,40*py,"Close",false,passport_info,nil,nil,nil,nil,nil,nil,tocolor(255,0,0),tocolor(100,0,0),tocolor(255,0,0))

-- b) 
addEventHandler("onDgsMouseClick",root,function(btn,state)
	if btn == "left" and state == "down" then
		if source == closeButton then
		showCursor(false) 
        	dgs:dgsSetVisible(passport_info,false)
        	on = 0
		end
	end
end)

-- server side 
-- if you changed the column names in your db you have to replace them in the query and insert statement 
-- old 
local query = dbQuery(connect,"SELECT * FROM Users WHERE firstname=? OR lastname=? OR accountName=?",tostring(firstname),tostring(lastname),acc_name)
-- new 
local query = dbQuery(connect,"SELECT * FROM Users WHERE FirstName=? OR LastName=? OR accountName=?",tostring(firstname),tostring(lastname),acc_name)

--- also this 
-- old 
dbExec(connect,"INSERT INTO Users (accountName,firstname,lastname,age,country) VALUES(?,?,?,?,?)",acc_name,firstname,lastname,age,country)
-- new 
dbExec(connect,"INSERT INTO Users (accountName,FirstName,LastName,Age,Country) VALUES(?,?,?,?,?)",acc_name,firstname,lastname,age,country)

Thank`s

I do not need to change the data when opening this panel
But at the moment, it can be done. How to remove it? How to make the data received from the DB (name, age, country) unchangeable? So they are static.

  • Like 1
Link to comment

Data from db is static it wont change unless you want to update it. It will be always the same because players cant make any changes. If you mean you can edit the client side memo you can do this to disable that.

https://wiki.multitheftauto.com/wiki/DgsMemoSetReadOnly

-- client side 
-- you look after this line of code
local CountryInfo = dgs:dgsCreateMemo(10,100,150,25,"Страна: "..rezult[1]['Country'].."",false,passport_info)
--after this line you add this lines of code
dgsMemoSetReadOnly(firstNameInfo,true) 
dgsMemoSetReadOnly(lirstNameInfo,true) 
dgsMemoSetReadOnly(ageInfo,true) 
dgsMemoSetReadOnly(CountryInfo,true) 

 

Link to comment
53 minutes ago, FlorinSzasz said:

Data from db is static it wont change unless you want to update it. It will be always the same because players cant make any changes. If you mean you can edit the client side memo you can do this to disable that.

https://wiki.multitheftauto.com/wiki/DgsMemoSetReadOnly

-- client side 
-- you look after this line of code
local CountryInfo = dgs:dgsCreateMemo(10,100,150,25,"Страна: "..rezult[1]['Country'].."",false,passport_info)
--after this line you add this lines of code
dgsMemoSetReadOnly(firstNameInfo,true) 
dgsMemoSetReadOnly(lirstNameInfo,true) 
dgsMemoSetReadOnly(ageInfo,true) 
dgsMemoSetReadOnly(CountryInfo,true) 

 

Okey, helped me.

Last question
Please help me find out how to call the "window" menu panel (passport registration) when in contact with a marker. I assume I need to write a function and addEventHendler.

 

local playerPassportEdit = createMarker(-712.9, 962.5, 12.3-1.7,"cylinder",2.0,255,0,0,104)


addEvent("playerPassportEdit",true)
addEventHandler("playerPassportEdit",getRootElement(),function()
    if source == playerPassportEdit  then
		dgs:dgsSetVisible(window,true)
		showCursor(true)
	end
end)

 

58 minutes ago, FlorinSzasz said:

Data from db is static it wont change unless you want to update it. It will be always the same because players cant make any changes. If you mean you can edit the client side memo you can do this to disable that.

https://wiki.multitheftauto.com/wiki/DgsMemoSetReadOnly

-- client side 
-- you look after this line of code
local CountryInfo = dgs:dgsCreateMemo(10,100,150,25,"Страна: "..rezult[1]['Country'].."",false,passport_info)
--after this line you add this lines of code
dgsMemoSetReadOnly(firstNameInfo,true) 
dgsMemoSetReadOnly(lirstNameInfo,true) 
dgsMemoSetReadOnly(ageInfo,true) 
dgsMemoSetReadOnly(CountryInfo,true) 

 

Please forgive my silly questions, but I really don't understand.

  • Like 1
Link to comment

Client Side we have to do this

Also you dont need openPanel or closePanel functions you can delete this functions and in first part of the script put all things in a function like i did.

local playerPassportEdit = createMarker(-712.9, 962.5, 12.3-1.7,"cylinder",2.0,255,0,0,104)
local on = 0

function registerWindow() 
  	if on == 1 then return end 
  	on = 1
  	window = dgs:dgsCreateWindow(((sw-800)/2)*px,((sh-600)/2)*py,800*px,600*py,"Паспорт",false)
	buttonExecute = dgs:dgsCreateButton(325*px,530*py,150*px,40*py,"Подтвердить",false,window,nil,nil,nil,nil,nil,nil,tocolor(255,0,0),tocolor(100,0,0),tocolor(255,0,0))

      labelFirstName = dgs:dgsCreateLabel(200*px,200*py,400*px,30*py," Имя: ",false,window)
      editFirstName = dgs:dgsCreateEdit(200*px,220*py,400*px,30*py,"",false,window,tocolor(255,255,255),nil,nil,nil)
dgs:dgsSetProperty(labelFirstName,"alignment",{"center"}, {"center"})
dgs:dgsSetProperty(editFirstName,"alignment",{"center"}, {"center"})

     labelLastName = dgs:dgsCreateLabel(200*px,270*py,400*px,30*py," Фамилия: ",false,window)
     editLastName = dgs:dgsCreateEdit(200*px,290*py,400*px,30*py,"",false,window,tocolor(255,255,255),nil,nil,nil)
dgs:dgsSetProperty(labelLastName,"alignment",{"center"}, {"center"})
dgs:dgsSetProperty(editLastName,"alignment",{"center"}, {"center"})

     labelAge = dgs:dgsCreateLabel(200*px,340*py,400*px,30*py," Возраст: ",false,window,tocolor(255,255,255),nil,nil,nil)
      editAge = dgs:dgsCreateEdit(200*px,360*py,400*px,30*py,"",false,window)
dgs:dgsSetProperty(labelAge,"alignment",{"center"}, {"center"})
dgs:dgsSetProperty(editAge,"alignment",{"center"}, {"center"})

     labelCountry = dgs:dgsCreateLabel(200*px,410*py,400*px,30*py," Страна: ",false,window,tocolor(255,255,255),nil,nil,nil,tocolor(100,100,100,100))
     editCountry = dgs:dgsCreateEdit(200*px,430*py,400*px,30*py,"",false,window)
dgs:dgsSetProperty(labelCountry,"alignment",{"center"}, {"center"})
dgs:dgsSetProperty(editCountry,"alignment",{"center"}, {"center"})


dgs:dgsWindowSetMovable(window,false)
dgs:dgsWindowSetSizable(window,false)
dgs:dgsWindowSetCloseButtonEnabled(window,false)  
showCursor(true)	
end 


function passportRegister ( hitPlayer, matchingDimension )
  	if hitPlayer == localPlayer and source == playerPassportEdit then 
	registerWindow() 
    end 
end
addEventHandler ( "onClientMarkerHit", getResourceRootElement(), passportRegister )

--------- here we need to add somehting --- 
addEventHandler("onDgsMouseClick",root,function(btn,state)
	if btn == "left" and state == "down" then
		if source == buttonExecute then
			local firstName = dgs:dgsGetText(editFirstName)
			local lastName = dgs:dgsGetText(editLastName)
			local age = dgs:dgsGetText(editAge)
			local country = dgs:dgsGetText(editCountry)
			if not string.find(firstName,"%S") then outputChatBox("Name") return end
			if not string.find(lastName,"%S") then outputChatBox("LastName") return end
			if not string.find(age,"%d") then outputChatBox("Age") return end
			if not string.find(country,"%S") then outputChatBox("Country") return end
			triggerServerEvent("playerPassportEnter",localPlayer,firstName,lastName,age,country)
        	-- also here we need this 
        	dgs:dgsSetVisible(window,false)
			showCursor(false)
        	on = 0
		end
	end
end)

 

Edited by FlorinSzasz
Link to comment
2 hours ago, FlorinSzasz said:

Client Side we have to do this

Also you dont need openPanel or closePanel functions you can delete this functions and in first part of the script put all things in a function like i did.

local playerPassportEdit = createMarker(-712.9, 962.5, 12.3-1.7,"cylinder",2.0,255,0,0,104)
local on = 0

function registerWindow() 
  	if on == 1 then return end 
  	on = 1
  	window = dgs:dgsCreateWindow(((sw-800)/2)*px,((sh-600)/2)*py,800*px,600*py,"Паспорт",false)
	buttonExecute = dgs:dgsCreateButton(325*px,530*py,150*px,40*py,"Подтвердить",false,window,nil,nil,nil,nil,nil,nil,tocolor(255,0,0),tocolor(100,0,0),tocolor(255,0,0))

      labelFirstName = dgs:dgsCreateLabel(200*px,200*py,400*px,30*py," Имя: ",false,window)
      editFirstName = dgs:dgsCreateEdit(200*px,220*py,400*px,30*py,"",false,window,tocolor(255,255,255),nil,nil,nil)
dgs:dgsSetProperty(labelFirstName,"alignment",{"center"}, {"center"})
dgs:dgsSetProperty(editFirstName,"alignment",{"center"}, {"center"})

     labelLastName = dgs:dgsCreateLabel(200*px,270*py,400*px,30*py," Фамилия: ",false,window)
     editLastName = dgs:dgsCreateEdit(200*px,290*py,400*px,30*py,"",false,window,tocolor(255,255,255),nil,nil,nil)
dgs:dgsSetProperty(labelLastName,"alignment",{"center"}, {"center"})
dgs:dgsSetProperty(editLastName,"alignment",{"center"}, {"center"})

     labelAge = dgs:dgsCreateLabel(200*px,340*py,400*px,30*py," Возраст: ",false,window,tocolor(255,255,255),nil,nil,nil)
      editAge = dgs:dgsCreateEdit(200*px,360*py,400*px,30*py,"",false,window)
dgs:dgsSetProperty(labelAge,"alignment",{"center"}, {"center"})
dgs:dgsSetProperty(editAge,"alignment",{"center"}, {"center"})

     labelCountry = dgs:dgsCreateLabel(200*px,410*py,400*px,30*py," Страна: ",false,window,tocolor(255,255,255),nil,nil,nil,tocolor(100,100,100,100))
     editCountry = dgs:dgsCreateEdit(200*px,430*py,400*px,30*py,"",false,window)
dgs:dgsSetProperty(labelCountry,"alignment",{"center"}, {"center"})
dgs:dgsSetProperty(editCountry,"alignment",{"center"}, {"center"})


dgs:dgsWindowSetMovable(window,false)
dgs:dgsWindowSetSizable(window,false)
dgs:dgsWindowSetCloseButtonEnabled(window,false)  
showCursor(true)	
end 


function passportRegister ( hitPlayer, matchingDimension )
  	if hitPlayer == localPlayer and source == playerPassportEdit then 
	registerWindow() 
    end 
end
addEventHandler ( "onClientMarkerHit", getResourceRootElement(), passportRegister )

--------- here we need to add somehting --- 
addEventHandler("onDgsMouseClick",root,function(btn,state)
	if btn == "left" and state == "down" then
		if source == buttonExecute then
			local firstName = dgs:dgsGetText(editFirstName)
			local lastName = dgs:dgsGetText(editLastName)
			local age = dgs:dgsGetText(editAge)
			local country = dgs:dgsGetText(editCountry)
			if not string.find(firstName,"%S") then outputChatBox("Name") return end
			if not string.find(lastName,"%S") then outputChatBox("LastName") return end
			if not string.find(age,"%d") then outputChatBox("Age") return end
			if not string.find(country,"%S") then outputChatBox("Country") return end
			triggerServerEvent("playerPassportEnter",localPlayer,firstName,lastName,age,country)
        	-- also here we need this 
        	dgs:dgsSetVisible(window,false)
			showCursor(false)
        	on = 0
		end
	end
end)

 

Bro, apologies for my stupidity, but I have another question. When I type text in the fields "FirstName, LastName...", pressing the "T" key opens the in-game chat. How can I get rid of this?
The "T" key is set to open the in-game chat, but this is very inconvenient.

  • Like 1
Link to comment

Interesting, i never had this problem.

You need to add this in registerWindow function

toggleControl("chatbox",false)

And also add this into your dgs mouse click EventHandler function.

if source == buttonExecute then -- after this line of code add the line of code i wrote below
toggleControl("chatbox",true)

I also want to say there is no stupidity you just want to do to much to fast, take your time and read the wiki and topics on forum. With the time you will learn faster or slower it doesnt matter, if you put a little bit of work you will learn something today, something tomorrow and so on.

https://forum.multitheftauto.com/forum/123-tutorials/

https://www.youtube.com/@ngear2872/videos

Edited by FlorinSzasz
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...