Hey developers! I am trying to assign a unique role to each player using the following code…
math.randomseed(os.time());
local function Shuffle(Array)
local Shuffled = {};
for _, Element in ipairs(Array) do
local Position = math.random(1, #Shuffled + 1);
table.insert(Shuffled, Position, Element);
end
return Shuffled
end
local Roles = {}
--local Players = Shuffle(game.Players:GetChildren()) [[DISABLED THIS FOR TESTING]]
local Players = {1,2,3,4,5,6,7,8,9}
Roles.Joker = Players[1]
Roles.Medic = Players[2]
Roles.Jury = {Players[3], Players[4]}
Roles.Detective = Players[5]
Roles.Assasin = Players[6]
Roles.Innocent = {}
for I = 7, #Players do
table.insert(Roles.Innocent, Players[I])
end
When I run the following code after the above, it prints all the roles, but in the order that they were specified. Joker prints 1, medic prints 2 etc.
for I, Value in pairs(Roles) do
print(tostring(I))
print(tostring(Value))
end
Does anyone know of a fix for this? Is it an issue with my code or with the shuffling function?
Thanks,
Winky.