Possibility of serial numbers not being diverse and causing error

I’m trying to make a tool-saving system where a tool and its attributes save. I decided to use serial numbers to make these items have a diverse index in a table.

The problem is, is that there is a very unlikely but possible chance where tools a player owns will be the same(sessionData[player][“Tools”]).

Important Information
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Remotes = ReplicatedStorage.Remotes
local Functions = ReplicatedStorage.Functions

local Items = ReplicatedStorage.Items
local Tools = Items.Tools

local ShopHitbox = workspace.ShopHitbox
local ShopEntrance = workspace.ShopEntrance
local ShopExit = workspace.ShopExit
local Interact = require(script.Interact)

local SerialNumbers = {}
local PlayersInShop = {}

Players.PlayerAdded:Connect(function(player)
	SerialNumbers[player] = {}
end)

local function InteractShop(hit)
	local Player = Players:GetPlayerFromCharacter(hit.Parent)
	if Player and not PlayersInShop[Player] then
		PlayersInShop[Player] = Player
		Interact.new(Player):Init()
		Remotes.ToServer.OnServerEvent:Connect(function(player,signal)
			local Character = player.Character
			local Root = Character.HumanoidRootPart
			local Humanoid = Character.Humanoid
			if signal == "ShopCutsceneEnabled" then
				Root.Position = ShopEntrance.Position
			end
		end)
	end
end

game.DescendantRemoving:Connect(function(desc)
	if desc:IsA("Tool") and desc:GetAttribute("SerialNumber") then
		for i,v in ipairs(SerialNumbers) do
			if v == desc:GetAttribute("SerialNumber") then
				table.remove(SerialNumbers,SerialNumbers[i])
			end
		end
	end
end)

local function Serialize(player)
	local sn = math.random(0,1e7)
	table.insert(SerialNumbers[player],sn)
	return math.random(0,1e7)
end

Functions.ShopPurchase.OnServerInvoke = function(player,signal,toolName)
	if signal == "PurchaseItem" then
		if typeof(toolName) ~= "string" then return end
		local tool = Tools:FindFirstChild(toolName)
		local price = tool:GetAttribute("Price")
		if player.leaderstats.Money.Value >= price then
			local newTool = tool:Clone()
			local serial = Serialize(player)
			newTool:SetAttribute("SerialNumber",serial)
			player.leaderstats.Money.Value -= price
			newTool.Parent = player.Backpack
			return "Success"
		else
			return "Failure"
		end
	end
end

local Shop = {}


function Shop:Init()
	ShopHitbox.Touched:Connect(InteractShop)
	Remotes.ToServer.OnServerEvent:Connect(function(player,signal)
		local character = player.Character
		local root = character.HumanoidRootPart
		local humanoid = character.Humanoid
		if signal == "ReverseShopChanges" then
			if PlayersInShop[player] then PlayersInShop[player] = nil end
			Interact:Reverse(humanoid)
			root.Position = ShopExit.Position
		end
	end)
end
return Shop

In the remote function you can see that I am trying to create a serial number for the new tool. I am creating a a variable = math.random(0,1e7).

I’ve tried using a while loop and repeat loop to check if the new serial number is not in the SerialNumbers table but whenever I have done that the serial has never returned.

Why not make a serial number that’s the player’s ID number + a decimal like .01?
Every time they add a tool you add another .01 and make that the S/N.
I’d suggest using math.round to make sure the decimals don’t get messed up and be something like 123456789.020000001. They shouldn’t since I don’t imagine you’re doing any math with the nubmers, but better to be safe.

This would only work up to 100 serial numbers, so if you do need more you can start at .001 and add .001 for every S/N after that.

1 Like

If I bought 2 tools from the shop though, they would both have the serial number player ID + 0.01.

HttpService:GenerateGUID()

1 Like

I see it’s been solved, but what I meant was start the beginning of the game with the (player ID number).01 S/N, then when any additional item was bought add .01 to the last item the player bought.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.