My goal is to give all players an IntValue as soon as they enter the game. The value cannot be given twice, meaning that two players cannot bear the same number in the Value property. And this is the problem I am having: how do I prevent the script from giving out the same value to 1+ players? I tried using table.remove with in pairs loop as well, but the fact that the loop also includes a player, who does not have a value assigned made this method unreliable. I have written many different variations of the script, code in line 14 is not finished, as I was not able to write a proper block content.
local PlayerIndexTable = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} --Values to be given out
local function PlayerAdded(player)
local Index = Instance.new("IntValue")
Index.Parent = player --Creates a value and parents it to a player
local PlayerNum = game.Players:GetPlayers()
if #PlayerNum == 1 then
Index.Value = math.random(#PlayerIndexTable) --If there is only one player, the value is chosen randomly
end
if #PlayerNum > 1 then --If more than one player then
local ChosenValue = math.random(#PlayerIndexTable)
for i,v in pairs(PlayerNum) do
if ChosenValue == v.Value.Value then --Checks if the chosen value is equal to that of other players
Index.Value
end
end
end
end
game.Players.PlayerAdded:Connect(PlayerAdded)
