Hello, please tell me how to make the id of the player who came to the server is stored in it, that is, it will restart in it is changing, I would like to make it so that when a player logged in his id for example 1 when you restart it the same 1
Allow me to provide an explanation based on my understanding of what you are trying to achieve.
In order to achieve this, you can store the player’s ID in a database or a file on the server, and then retrieve it when the player logs in again. Here is an example of how you can do this using a file:
- Create a file in your server’s resources folder, let’s call it “player_ids.txt”.
- In your server-side script, when a player logs in, read the file to check if their ID exists in it. If it does, set the player’s ID to that value. If not, generate a new ID and write it to the file. Here is an example:
local playerIds = {}
local playerCount = 0
-- Read the file to get existing player IDs
local file = io.open("resources/player_ids.txt", "r")
if file then
for line in file:lines() do
table.insert(playerIds, tonumber(line))
playerCount = playerCount + 1
end
file:close()
end
-- Assign an ID to the new player
local playerId
if playerCount > 0 then
-- Use the last player ID + 1
playerId = playerIds[playerCount] + 1
else
-- No players yet, start at 1
playerId = 1
end
-- Save the new ID to the file
file = io.open("resources/player_ids.txt", "a")
file:write(playerId .. "\n")
file:close()
-- Set the player's ID
TriggerClientEvent("setPlayerId", playerId)
- In your client-side script, listen for the “setPlayerId” event and save the player’s ID in a variable. Here is an example:
local playerId
RegisterNetEvent("setPlayerId")
AddEventHandler("setPlayerId", function(id)
playerId = id
end)
Now, whenever the player logs in, they will be assigned a unique ID that will persist across server restarts. You can use this ID to store and retrieve player data as needed. Let me know if you have any additional questions, hope this helps!
Maybe I did not understand something wrong, I inserted what you sent me, created a text file, but the ID is still changing after every re-entry
I apologize for my mistake. I misunderstood your objective and thought that you were seeking a method to log the ID entries instead of retaining them. Nevertheless, as far as I know, it is not possible to permit the player to maintain the same ID, unless someone with more knowledge indicates otherwise.
share my code with us I’ve been trying to make this work for a long time and without success, most of the community is not interested in leaving the id static… I’ll be very grateful to you.
yes this is possible because there are already servers with this system so we don’t know how to do it, servers with vrp system come with it by default
Ive been looking to do that on qbcore for months, I hope someone figures it out
cosimc king of the hill have this system
tôi làm được điều này nó thật đơn giản
It would be nice if you could do