Creating the number

Hello,

I have the script for phone but the number does not automatically create in the database …

1 Like

Alright, well too bad.

What do you actually expect ?

Either post the code or don’t make a topic at all.

The start of the phone script in the server.lua file

function checkNumber(number)
    local executed_query = MySQL:executeQuery("SELECT * FROM users WHERE phone_number = '@number' LIMIT 1", { ['@number'] = number })
    local result = MySQL:getResults(executed_query, { 'identifier','name'})
    if result then
        for _, v in ipairs(result) do
            return v
        end

    else
        return false
    end


end

RegisterServerEvent("pm:addNewNumero")
AddEventHandler("pm:addNewNumero", function(number)
    local player = getPlayerID(source)
    local contact =  checkNumber(number)
    if not contact then
        TriggerClientEvent("pm:notifs", source, "~o~Aucun contact trouvé")
    else
        local executed_query = MySQL:executeQuery("SELECT * FROM user_phonelist WHERE owner_id = '@username' AND contact_id = '@id' ", { ['@username'] = player, ['@id'] = contact.identifier })
        local result = MySQL:getResults(executed_query, { 'contact_id' })
        print(json.encode(result[1]))
        if(result[1] == nil) then
            MySQL:executeQuery("INSERT INTO user_phonelist (`owner_id`, `contact_id`) VALUES ('@owner', '@contact')",
                { ['@owner'] = player, ['@contact'] = contact.identifier })
            TriggerClientEvent("pm:notifs", source, "~g~Numéro de ~y~".. contact.name .. " ~g~ajouté" )
            updateRepertory({source = source, player = player })
        else
            TriggerClientEvent("pm:notifs", source, " ~y~".. contact.name .. "~r~ existe déjà dans votre répertoire" )
        end
    end
end)

RegisterServerEvent("pm:checkContactServer")
AddEventHandler("pm:checkContactServer", function(identifier)
    print(json.encode(identifier))
    local executed_query = MySQL:executeQuery("SELECT * FROM users WHERE identifier = '@id'", { ['@id'] = identifier.identifier })
    local result = MySQL:getResults(executed_query, { 'identifier', 'phone_number', 'name' })

    if result[1] ~= nil then
        for _, v in ipairs(result) do
            TriggerClientEvent("pm:notifs", source, "~o~".. v.name .. " : ~s~" .. v.phone_number)
        end
    end

end)




function updateRepertory(player)
    numberslist = {}
    source = player.source
    local player = player.player
    local executed_query = MySQL:executeQuery("SELECT * FROM user_phonelist JOIN users ON `user_phonelist`.`contact_id` = `users`.`identifier` WHERE owner_id = '@username' ORDER BY name ASC", { ['@username'] = player })
    local result = MySQL:getResults(executed_query, { 'identifier','name', 'contact_id'}, "contact_id")
    if (result) then
        for _, v in ipairs(result) do
            t = { name= v.name, identifier = v.identifier }
            table.insert(numberslist, v.identifier, t)
        end
    end
    TriggerClientEvent("pm:repertoryGetNumberListFromServer", source, numberslist)
end

local numberslist = {}
RegisterServerEvent("pm:repertoryGetNumberList")
AddEventHandler("pm:repertoryGetNumberList", function()
    numberslist = {}
    local player = getPlayerID(source)
    local executed_query = MySQL:executeQuery("SELECT * FROM user_phonelist JOIN users ON `user_phonelist`.`contact_id` = `users`.`identifier` WHERE owner_id = '@username' ORDER BY name ASC", { ['@username'] = player })
    local result = MySQL:getResults(executed_query, { 'identifier','name', 'contact_id'}, "contact_id")
    if (result) then
        for _, v in ipairs(result) do
            t = { name= v.name, identifier = v.identifier }
            table.insert(numberslist, v.identifier, t)
        end
    end
    TriggerClientEvent("pm:repertoryGetNumberListFromServer", source, numberslist)
end)

RegisterServerEvent("pm:sendNewMsg")
AddEventHandler("pm:sendNewMsg", function(msg)
    msg = {
        owner_id = getPlayerID(source),
        receiver = msg.receiver,
        msg = msg.msg
    }
    MySQL:executeQuery("INSERT INTO user_message (`owner_id`, `receiver_id`, `msg`, `has_read`) VALUES ('@owner', '@receiver', '@msg', '@read')",
        { ['@owner'] = msg.owner_id, ['@receiver'] = msg.receiver, ['@msg'] = msg.msg, ['@read'] = 0 })
    TriggerClientEvent("pm:notifs", source, " ~g~ message envoyé" )
end)

local messagelist = {}
RegisterServerEvent("pm:messageryGetOldMsg")
AddEventHandler("pm:messageryGetOldMsg", function()
    messagelist = {}
    local player = getPlayerID(source)
    local executed_query = MySQL:executeQuery("SELECT * FROM user_message JOIN users ON `user_message`.`owner_id` = `users`.`identifier` WHERE receiver_id = '@user'", { ['@user'] = player })
    local result = MySQL:getResults(executed_query, { 'identifier', 'name', 'msg', 'date', 'has_read', 'owner_id', 'receiver_id'})
    if (result) then
        for _, val in ipairs(result) do
            message = { msg = val.msg, name = val.name, identifier = val.identifier, date = tostring(val.date), has_read = val.has_read }
            --table.insert(messagelist, val.identifier, message)
            messagelist[_] = message
        end

    end
    TriggerClientEvent("pm:messageryGetOldMsgFromServer", source, messagelist)
end)

RegisterServerEvent("pm:setMsgReaded")
AddEventHandler("pm:setMsgReaded", function(msg)
    print(json.encode(msg))
    MySQL:executeQuery("UPDATE user_message SET `has_read` = 1 WHERE `receiver_id` = '@receiver' AND `msg` = '@msg' AND `has_read` = '@read' ", { ['@receiver'] = getPlayerID(source), ['@msg'] = msg.msg, ['@read'] = msg.has_read })
end)

The start of the phone script in the client.lua file

NUMBERS_LIST = {}
OLDS_MSG = {}
AddEventHandler("playerSpawned", function()
    TriggerServerEvent("pm:repertoryGetNumberList")
    TriggerServerEvent("pm:messageryGetOldMsg")
end)

Citizen.CreateThread(function()
    while true do
        Wait(30000)
        TriggerServerEvent("pm:messageryGetOldMsg")
    end
end)

-- handles when a player spawns either from joining or after death
RegisterNetEvent("pm:repertoryGetNumberListFromServer")
AddEventHandler("pm:repertoryGetNumberListFromServer", function(NUMBERSLIST)
    NUMBERS_LIST = {}
    NUMBERS_LIST = NUMBERSLIST
end)

-- handles when a player spawns either from joining or after death
RegisterNetEvent("pm:notifs")
AddEventHandler("pm:notifs", function(msg)
    notifs(msg)
end)

function notifs(msg)
    SetNotificationTextEntry("STRING")
    AddTextComponentString( msg )
    DrawNotification(false, false)
end

-- handles when a player spawns either from joining or after death
RegisterNetEvent("pm:messageryGetOldMsgFromServer")
AddEventHandler("pm:messageryGetOldMsgFromServer", function(OLDSMSG)
    OLDS_MSG = {}
    OLDS_MSG = OLDSMSG
end)

-- Menu du téléphone

function phoneMenu()
    options.menu_subtitle = "Telephone"
    ClearMenu()
    Menu.addButton("Ajouter un numero", "newNumero", nil )
    Menu.addButton("Repertoire", "repertoryMenu", nil )
    Menu.addButton("Messagerie", "messageryMenu", nil )
    Menu.addButton("Prendre une photo", "takePhoto", nil )
    Menu.addButton("Retour", "PersonnalMenu", nil )
end

function newNumero()
    DisplayOnscreenKeyboard(1, "FMMC_KEY_TIP8", "", "", "", "", "", 10)
    while (UpdateOnscreenKeyboard() == 0) do
        DisableAllControlActions(0);
        Wait(0);
    end
    if (GetOnscreenKeyboardResult()) then
        local result = GetOnscreenKeyboardResult()
        TriggerServerEvent("pm:addNewNumero", result)
        phoneMenu()
    end
end

function repertoryMenu()
    options.menu_subtitle = "Repertoire"
    ClearMenu()
    Menu.addButton("Appeler la police (Bientôt)", "callPolice")
    for ind, value in pairs(NUMBERS_LIST) do
        Menu.addButton(value.name, "repertoryContact", value.identifier)
    end
    Menu.addButton("Retour", "phoneMenu", nil )
end

function callPolice()
end

function repertoryContact(contact)
    options.menu_subtitle = "Repertoire"
    ClearMenu()
    Menu.addButton("Afficher le numéro", "checkContact", contact )
    Menu.addButton("Envoyer un message", "writeMsg", contact )
    Menu.addButton("Retour", "phoneMenu", nil )

end

function checkContact(contact)
    Citizen.Trace("DEBUG: "..contact)
    TriggerServerEvent("pm:checkContactServer", {identifier = contact})
end

function writeMsg(receiver)

    DisplayOnscreenKeyboard(6, "FMMC_KEY_TIP8", "(120 characters max)", "", "", "", "", 120)
    while (UpdateOnscreenKeyboard() == 0) do
        DisableAllControlActions(0);
        Wait(0);
    end
    if (GetOnscreenKeyboardResult()) then
        local result = GetOnscreenKeyboardResult()
        local msg = {
            receiver = receiver,
            msg = result
        }
        TriggerServerEvent("pm:sendNewMsg", msg)
        phoneMenu()
    end
end

function messageryMenu()
    options.menu_subtitle = "Messagerie"
    ClearMenu()
    for ind, value in pairs(OLDS_MSG) do
        local n = ""
        if value.has_read == 0 then
            n = " - ~r~Non lu"
            notifs("~o~ Vous avez des nouveaux message non lus !")
        end
        Menu.addButton(value.name .. " " .. n, "readOldMsg", {msg = value.msg, name = value.name, date= value.date, has_read = value.has_read, receiver_id = value.receiver_id})
    end
    Menu.addButton("Retour", "phoneMenu", nil )
end

function readOldMsg(msg)
    if msg.has_read == 0 then
        TriggerServerEvent("pm:setMsgReaded", msg)
    end
    SetNotificationTextEntry("STRING")
    AddTextComponentString(msg.msg)
    SetNotificationMessage("CHAR_ALL_PLAYERS_CONF", "CHAR_ALL_PLAYERS_CONF", true, 1, msg.name , "Le ("..tostring(msg.date)..")" , "Message reçu")
    DrawNotification(false, true)
    messageryMenu()
end

function takePhoto()
    RequestAnimDict( "cellphone@" )
    while not HasAnimDictLoaded( "cellphone@" ) do
        Citizen.Wait(0)
    end
    if HasAnimDictLoaded( "cellphone@" ) then
        TaskPlayAnim( PlayerPedId(), "cellphone@" , "cellphone_photo_idle" ,8.0, -8.0, -1, 0, 0, false, false, false )
    end
end

It does not have the function to create the number if the player steps in when the player logs in

You want create automatically a random number for user in the db?

A random number between 10000 - 99999

Style:

	local number = math.random(1000, 9999)
	"insertion in the db code ("555-"..number.." ")

#Exemple code, not working #BaguetteCode

Yes that’s it!

Because #PoutineAvecDeLaSauce

1 Like

I have a part of a code on another phone script but I do not know how to put it to mine etc …

local function GenerateUniquePhoneNumber()

	local foundNumber = false
	local phoneNumber = nil

	while not foundNumber do

		phoneNumber = math.random(10000, 99999)

		local executed_query = MySQL:executeQuery("SELECT COUNT(*) as count FROM users WHERE phone_number = '@phoneNumber'", {['@phoneNumber'] = phoneNumber})
		local result         = MySQL:getResults(executed_query, {'count'})
		local count          = tonumber(result[1].count)

		if count == 0 then
			foundNumber = true
		end

	end

	return phoneNumber

end

AddEventHandler('onResourceStart', function(ressource)
	if ressource == 'esx_phone' then
		TriggerEvent('esx_phone:ready')
	end
end)

AddEventHandler('es:playerLoaded', function(source)

	local _source = source

	TriggerEvent('es:getPlayerFromId', source, function(xPlayer)

		local executed_query = MySQL:executeQuery("SELECT * FROM users WHERE identifier = '@identifier'", {['@identifier'] = xPlayer.identifier})
		local result         = MySQL:getResults(executed_query, {'phone_number'})
		local phoneNumber    = result[1].phone_number

		if phoneNumber == nil then
			phoneNumber = GenerateUniquePhoneNumber()
			MySQL:executeQuery("UPDATE users SET phone_number = '@phone_number' WHERE identifier = '@identifier'", {['@identifier'] = xPlayer.identifier, ['@phone_number'] = phoneNumber})
		end

		xPlayer['phoneNumber'] = phoneNumber

		local contacts = {}

		local executed_query2 = MySQL:executeQuery("SELECT * FROM user_contacts WHERE identifier = '@identifier'", {['@identifier'] = xPlayer.identifier})
		local result2         = MySQL:getResults(executed_query2, {'name', 'number'})

		for i=1, #result2, 1 do
			
			table.insert(contacts, {
				name   = result2[i].name,
				number = result2[i].number,
				type   = 'player'
			})

		end

		xPlayer['contacts'] = contacts

		TriggerClientEvent('esx_phone:loaded', _source, phoneNumber, contacts)

	end)
end)

Go test it

I do not know if it works: /

SERVER

RegisterServerEvent('cp:spawnplayer')
AddEventHandler('cp:spawnplayer', function()
	TriggerEvent('es:getPlayerFromId', source, function(user)
	local player = user.identifier
	local numberofphone = math.random(1000, 9999)
	local executed_query = MySQL:executeQuery("UPDATE phone_number SET `phone_number` = '@number' WHERE identifier = '@username'", {['@number'] = numberofphone, ['@username'] = player})
	end)
 end)

Client

AddEventHandler("playerSpawned", function(spawn)
    TriggerServerEvent("cp:spawnplayer")	
end)

Not work

And this?:

Server

RegisterServerEvent('cp:spawnplayer')
AddEventHandler('cp:spawnplayer', function()
	TriggerEvent('es:getPlayerFromId', source, function(user)
 
	local foundNumber = false
	local phoneNumber = nil

	while not foundNumber do

		local phoneNumber = math.random(1000, 9999)

		local executed_query = MySQL:executeQuery("SELECT COUNT(*) as count FROM users WHERE phone_number = '@phoneNumber'", {['@phoneNumber'] = phoneNumber})
		local result = MySQL:getResults(executed_query, {'count'})
		local count = tonumber(result[1].count)

		if count == 0 then
			foundNumber = true
		end

	end

	return phoneNumber
 
	end)
end)

Client

AddEventHandler("playerSpawned", function(spawn)
    TriggerServerEvent("cp:spawnplayer")	
end)

Not always null, to you teamspeak?

isn’t this from github?

Yes but I’m busy :confused: Tomorrow ?

Yes it is on github the script?

Give me the link of this script please

Add on server.lua

 RegisterServerEvent('cp:spawnplayer')
 AddEventHandler('cp:spawnplayer', function()
 	TriggerEvent('es:getPlayerFromId', source, function(user)
 	local player = user.identifier
 	local numberofphone = math.random(1000, 9999)
	local executed_query = MySQL:executeQuery("UPDATE users SET `phone_number` = '@number' WHERE identifier = '@username'", {['@number'] = numberofphone, ['@username'] = player})
 	end)
  end)

and Clinet.lua

 AddEventHandler("playerSpawned", function(spawn)
    TriggerServerEvent("cp:spawnplayer")	
 end)

Its worked :slight_smile:

Thanks for the correction but if the number exist, the number will be recreated :confused:

Hey Magistrox,

Could you put all the code here please ? They 're is function missing to make work the phone.

And after that, we maybe could help to get some random phone numbers.

Hello please what is the database to enter? thank you in advance

What are the tables created in the database?