DevNaka
2
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!