Having issues with shuffling an array

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.

why you start from I = 7?
for I = 7, #Players do
table.insert(Roles.Innocent, Players[I])
end
You want 7 players innocent?

From the 7th argument of the players table and onwards, the players will be marked as innocent because the first 7 players have been given a role that is not innocent.

I can’t understand problem… You need change order?

The problem is, I want the roles to be randomized. At the moment, the person with the first name alphabetically will be Joker, then the next will be the Medic and so on. I am trying to randomize the Players array so that everyone has an equal chance of being each role.

why you can’t make array with roles and mix roles?

I can’t really because then the leftover players wouldn’t be Innocent and I would still need the shuffling function that I created this thread with the intention of finding.

Ok, wait i will fix this now. .

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 RolesToSort = {}

Roles.Joker = {}
Roles.Medic = {}
Roles.Jury = {}
Roles.Detective = {}
Roles.Assasin = {}
Roles.Innocent = {}

local Players = {1,2,3,4,5,6,7,8,9}
local function SortToArrays(ArrayHere)
for i,v in pairs(ArrayHere) do
if v == “Joker” then
Roles.Joker = Players[i]
end
if v == “Medic” then
Roles.Medic = Players[i]
end
if v == “Jury” then
Roles.Jury[#Roles.Jury + 1] = Players[i]
end
if v == “Detective” then
Roles.Detective = Players[i]
end
if v == “Assasin” then
Roles.Assasin = Players[i]
end
if v == “Innocent” then
Roles.Innocent[#Roles.Innocent + 1] = Players[i]
end
end
end

–local Players = Shuffle(game.Players:GetChildren()) [[DISABLED THIS FOR TESTING]]
local RolesExists = {“Joker”,“Medic”,“Jury”,“Jury”,“Detective”,“Assasin”,“Innocent”,“Innocent”,“Innocent”}
RolesExists = Shuffle(RolesExists)

print(“COUNT:”…#Players)
for i = 1, #Players do
local RandomRole = math.random(1,#RolesExists)
RolesToSort[i] = RolesExists[i]
end

SortToArrays(RolesToSort)

for i,v in pairs(Roles.Innocent) do
print(“Innocents:”…v)
end
print(“Joker:”…Roles.Joker)
print(“Medic:”…Roles.Medic)
for i,v in pairs(Roles.Jury) do
print(“Jury:”…v)
end
print(“Detective:”…Roles.Detective)
print(“Assasin:”…Roles.Assasin)
–I hope this will help (Its morning for me and i can’t good understand)

As I said earlier,

Your script is just a less efficient version of mine and it is still using the shuffle function that doesn’t work.

Man,all is work, what you mean?

You forgot to shuffle the table.

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 = {}

-- shuffle the array
local Players = Shuffle({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

for k, v in pairs(Roles) do
	print(tostring(k))
	print(tostring(v))
end

Yeah, found that one.

It always sucks when you spend so long debugging something and you have no idea what’s wrong but it’s something so small and dumb.

Thanks for that!

Glad to hear it solved your problem! :wink: Please mark it as a solution so whenever someone has the same problem, they can find your post.

If you read my reply you’d see that it wasn’t your post that solved my problem but rather that I found it myself.

And I don’t think anyone will have an issue as specific as I did, with forgetting to call a shuffle function. Please stop asking for solutions.

1 Like

He mean that you need mark as soultion ur answer.

I found this method of shuffling an array or tables is it? from me following a tutorial on how to make a Music Player

for i = 1, #queue - 1 do -- queue as in your table name obviously
		local r = math.random(i,#queue)
		queue[i], queue[r] = queue[r], queue[i]

I really dont know how to explain this but its called Fisher-Yates Shuffle
Also big thanks to the guy making this music player tutorial Really appreciated his work :smiley:

4 Likes

More useful than the rest of this thread, thanks!

2 Likes