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.