Choosing a random killer

Hey! I was trying to remake a game I stopped working on and I stumbled against a bug.
The bug was the killer was always the same everytime unless someone left.

I tried fixing this a long time ago, and I just came back to this. I got some help and I made this:

for i, plr in pairs(Players:GetChildren()) do
			local char = plr.Character
			local humanRoot = char:WaitForChild("HumanoidRootPart")

			local RedSpawns = mapClone.RedSpawns:GetChildren()
			local BlueSpawns = mapClone.BlueSpawns:GetChildren()

			-- picks a random spawn from each team
			local randomRedSpawn = RedSpawns[math.random(1,#RedSpawns)]
			local randomBlueSpawn = BlueSpawns[math.random(1,#BlueSpawns)]
			-- will put the current player into a team with the less amount of players
				local PlayerList = {}

				local PreviousKiller : Player? = nil

				local function PickKiller()
					for _, player : Player in game:GetService("Players"):GetPlayers() do
						if player ~= PreviousKiller then
							table.insert(PlayerList, player)
						end
					end
					local RandomIndex = math.random(1, #PlayerList)
					local SelectedKiller = PlayerList[RandomIndex]
					PreviousKiller = SelectedKiller
					table.clear(PlayerList)

					return SelectedKiller
				end

				local killer = PickKiller()
				if plr ~= killer then
					plr.Team = Blue
					humanRoot.CFrame = randomBlueSpawn.CFrame + Vector3.new(math.random(1,3),math.random(1,3),math.random(1,3))
				plr.CameraMode = Enum.CameraMode.Classic
					plr.CameraMaxZoomDistance = 9

					table.insert(BlueTeamCount, plr.Name)
				elseif plr == killer then
					plr.Team = Red
				plr.CameraMode = Enum.CameraMode.LockFirstPerson
					plr.CameraMaxZoomDistance = 9

					humanRoot.CFrame = randomBlueSpawn.CFrame + Vector3.new(math.random(1,3),math.random(1,3),math.random(1,3))

				table.insert(RedTeamCount, plr.Name)
			end
			wait(0.2)
			if #RedTeamCount == 1 then

				plr.Team = Blue
				-- this is so we can make the spawn location for each player more randomized
				humanRoot.CFrame = randomBlueSpawn.CFrame + Vector3.new(math.random(1,3),math.random(1,3),math.random(1,3))
				plr.CameraMode = "Classic"
				plr.CameraMaxZoomDistance = 9
				table.insert(BlueTeamCount, plr.Name)
				wait(0.1)
			elseif #RedTeamCount == 0 then

				plr.Team = Red
				plr.CameraMode = Enum.CameraMode.LockFirstPerson
				plr.CameraMaxZoomDistance = 0.5
				humanRoot.CFrame = randomRedSpawn.CFrame + Vector3.new(math.random(1,3),math.random(1,3),math.random(1,3))

				table.insert(RedTeamCount, plr.Name)
			end

But this problem solver was buggy, sometimes there where two killers, and there was a pattern where everyone would be runner or killer.

This was the original, it was working but the killer is the same

		local BlueTeamCount = {}
		local RedTeamCount = {}

		for i, plr in pairs(Players:GetChildren()) do
			local char = plr.Character
			local humanRoot = char:WaitForChild("HumanoidRootPart")

			local RedSpawns = mapClone.RedSpawns:GetChildren()
			local BlueSpawns = mapClone.BlueSpawns:GetChildren()

			-- picks a random spawn from each team
			local randomRedSpawn = RedSpawns[math.random(1,#RedSpawns)]
			local randomBlueSpawn = BlueSpawns[math.random(1,#BlueSpawns)]
			-- will put the current player into a team with the less amount of players
			if #RedTeamCount == 1 then

				plr.Team = Blue
				-- this is so we can make the spawn location for each player more randomized
				humanRoot.CFrame = randomBlueSpawn.CFrame + Vector3.new(math.random(1,3),math.random(1,3),math.random(1,3))
				plr.CameraMode = "Classic"
				plr.CameraMaxZoomDistance = 9
				table.insert(BlueTeamCount, plr.Name)
				wait(0.1)
			elseif #RedTeamCount == 0 then

				plr.Team = Red
				plr.CameraMode = Enum.CameraMode.LockFirstPerson
				plr.CameraMaxZoomDistance = 0.5
				humanRoot.CFrame = randomRedSpawn.CFrame + Vector3.new(math.random(1,3),math.random(1,3),math.random(1,3))

				table.insert(RedTeamCount, plr.Name)
			else
				
				local randTeam = math.random(1,2)
				if randTeam == 1 then
					plr.Team = Blue
					humanRoot.CFrame = randomBlueSpawn.CFrame + Vector3.new(math.random(1,3),math.random(1,3),math.random(1,3))
				plr.CameraMode = Enum.CameraMode.Classic
					plr.CameraMaxZoomDistance = 9

					table.insert(BlueTeamCount, plr.Name)
				elseif randTeam == 2 then
					plr.Team = Red
				plr.CameraMode = Enum.CameraMode.LockFirstPerson
					plr.CameraMaxZoomDistance = 9

					humanRoot.CFrame = randomBlueSpawn.CFrame + Vector3.new(math.random(1,3),math.random(1,3),math.random(1,3))

					table.insert(RedTeamCount, plr.Name)
				end
			end
			wait(0.2)
			char:WaitForChild("Humanoid").Died:Connect(function()

				plr.Team = Neutrual
				plr.CameraMode = Enum.CameraMode.Classic
				plr.CameraMaxZoomDistance = 9
			end)
		end	
	end	
end)

Is the goal just to put one randomly-selected player on the red team, and the rest of the players on the blue team?

I think this works I don’t exactly know:

				local PlayerList = {}

				local PreviousKiller : Player? = nil

				local function PickKiller()
					for _, player : Player in game:GetService("Players"):GetPlayers() do
						if player ~= PreviousKiller then
							table.insert(PlayerList, player)
						end
					end
					local RandomIndex = math.random(1, #PlayerList)
					local SelectedKiller = PlayerList[RandomIndex]
					PreviousKiller = SelectedKiller
					table.clear(PlayerList)

					return SelectedKiller
				end

				local killer = PickKiller()
				if plr ~= killer then
					plr.Team = Killer
					-- this is so we can make the spawn location for each player more randomized
					humanRoot.CFrame = randomInnocent

The goal is to choose one killer and keep the rest in the other team. Killer is the team that only has one player. Runner is the team with everyone

Okay! I think the code you originally posted was adapted from a more general-purpose team shuffling algorithm.

We might be able to simplify things a bit. Can you describe exactly what all your requirements are? For example, it looks like you have some logic to prevent the same killer being chosen twice. How should that work? How should the spawn locations work?

1 Like