I am trying to give players a special number, that cannot be owned by anyone else. However, I ran into a problem: I cannot figure out how to check if the chosen number (all numbers are stored in a table) is already taken by someone else. I tried using code in line 14, but I then realised that I am asking whether ALL players have the chosen value, which is not the case. The question is: how can I check, if the generated number is already owned?
local PlayerIndexTable = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
local function PlayerAdded(player)
local PlayerNum = game.Players:GetChildren()
if #PlayerNum == 1 then
local Index = Instance.new("IntValue")
Index.Parent = player
Index.Value = math.random(#PlayerIndexTable)
end
if #PlayerNum > 1 then
for i,v in pairs(PlayerNum) do
if v:FindFirstChild("Value") ~= nil then --excludes player(s) with value
local ChosenValue = next(PlayerIndexTable, i)
if ChosenValue ~= v.Value.Value and ChosenValue ~= 0 then
local Index = Instance.new("IntValue")
Index.Parent = player
Index.Value = ChosenValue
break
else
print("Value repeated")
end
end
end
end
end
game.Players.PlayerAdded:Connect(PlayerAdded)
