Murder Mystery script sometimes letting the wrong side win. How would i fix it?

So basically I made a Murder Mystery script but sometimes the wrong side wins. Example: The murderer dies but then the murderer wins. Another example: The murderer kills everyone but then the murderer loses

I ofc can’t find anything on the devforum cause yk. Also I tried on my own.

heres my script:

function scriptfunnyhaha()
	local intermission = 10
	local roundLength = 180

	local inRound = game.ReplicatedStorage.InRound
	local status = game.ReplicatedStorage.Status
	local playing = {}
	local playingTeam = game.Teams.Playing
	local playersingame = 0
	local murdingamechecked = {}
	local lobbyTeam = game.Teams.Lobby

	local murdermodule = require(game.ReplicatedStorage.MurderModule)

	inRound.Changed:Connect(function()

		if inRound.Value == true then

			for i, plr in pairs(game.Players:GetChildren()) do

				local char = plr.Character
				local humanRoot = char:WaitForChild("HumanoidRootPart")



				plr.Team = playingTeam

				char:WaitForChild("Humanoid").Died:Connect(function(playerwhodied)

					plr.Team = lobbyTeam
					table.remove(playing, playerwhodied)

				end)

			end	


		else

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

				plr.Team = lobbyTeam

				human:UnequipTools()
				for i, tool in pairs(plr.Backpack:GetChildren()) do

					if tool.Name == "6 gold chain guns" then

					else
						plr.Backpack:ClearAllChildren()
					end	
				end
			end
		end	
	end)



	local function round()	
		while true do

			local requiredPlayers = 2

			repeat
				wait(1)
				status.Value = "Atleast ".. requiredPlayers.." players are needed to start a round"

			until #game.Players:GetChildren() >= requiredPlayers

			inRound.Value = false


			for i = intermission, 0, -1 do

				status.Value = "Game will start in "..i.." seconds"

				wait(1)
			end
			inRound.Value = true
			for i, plr in pairs(game.Players:GetChildren()) do

				if plr.Team.Name == "Playing" then


					table.insert(playing, plr.Name)


				end	
			end
			playersingame = #game.Players:GetPlayers()
			murdermodule.ChooseRoles()
			for i = roundLength, 0, -1 do

				status.Value = "Game will end in "..i.." seconds"



				local murdereralive = false
				local nonmurderersalive = false
				local murderer
				print(murderer)
				for i, playerInRound in pairs(playing) do
					if game.Players:FindFirstChild(playerInRound) then
						if game.Players:FindFirstChild(playerInRound):WaitForChild("Role").Value == "Murderer" then
							murdereralive = true
							murderer = game.Players:FindFirstChild(playerInRound)
							print("murd is alive")
						elseif game.Players:FindFirstChild(playerInRound):WaitForChild("Role").Value ~= "Murderer" then
							nonmurderersalive = true
							print("innocents are alive")	
						end

					else
						if murderer == playerInRound then
							status.Value = "The Murderer has turned themselves in."
							if game.Workspace:FindFirstChild("GunClone") then
								game.Workspace.GunClone:Destroy()
								if game.Workspace:FindFirstChild("GunClone") then
									game.Workspace.GunClone:Destroy()
								end
							end
							wait(3)
							return scriptfunnyhaha()
						end
					end

				end

				if #playing == 0 then

					status.Value = "Everyone Has Died"
					if game.Workspace:FindFirstChild("GunClone") then
						game.Workspace.GunClone:Destroy()
						if game.Workspace:FindFirstChild("GunClone") then
							game.Workspace.GunClone:Destroy()
						end
					end
					wait(3)
					return scriptfunnyhaha()

				end
				print(tostring(murdereralive).. "murd")
				print(tostring(nonmurderersalive).. "nonmurd")
				if murdereralive == true and nonmurderersalive == false then
					status.Value = "The Murderer got away with their crimes."
					if game.Workspace:FindFirstChild("GunClone") then
						game.Workspace.GunClone:Destroy()
						if game.Workspace:FindFirstChild("GunClone") then
							game.Workspace.GunClone:Destroy()
						end
					end

					wait(3)
					return scriptfunnyhaha()
				end

				if murdereralive == false and nonmurderersalive == true then
					status.Value = "The Murderer has been rightfully killed!"
					if game.Workspace:FindFirstChild("GunClone") then
						game.Workspace.GunClone:Destroy()
						if game.Workspace:FindFirstChild("GunClone") then
							game.Workspace.GunClone:Destroy()
						end
					end
					wait(3)
					return scriptfunnyhaha()
				end

				if nonmurderersalive == true and i == 0 then
					status.Value = "The Murderer took too long and got caught."
					if game.Workspace:FindFirstChild("GunClone") then
						game.Workspace.GunClone:Destroy()
						if game.Workspace:FindFirstChild("GunClone") then
							game.Workspace.GunClone:Destroy()
						end
					end

					wait(3)
					return scriptfunnyhaha()
				end

				wait(1)
			end
			wait(3)
		end
	end



	spawn(round)
end
scriptfunnyhaha()
1 Like

You’re only looking at a single person’s role inside of the script? The sometimes part being that person not being or being murderer

game.Players:FindFirstChild(playerInRound):WaitForChild("Role").Value
1 Like

It’s in a for loop which loops through everyone playing.

1 Like

Also I get what you mean so I’ll try to fix it hold on.

Actually I don’t know how I would do it though.

How would I do it? I can’t seem to figure it out.

Can somebody else help me with it? Since he seems to not be online.

Someone please help, I really don’t wanna wait.

where is the script where the player wins/loses? Here it’s about starting the round

oh wait i posted the wrong script sorry,
here it is

function scriptfunnyhaha()
	local intermission = 10
	local roundLength = 180

	local inRound = game.ReplicatedStorage.InRound
	local status = game.ReplicatedStorage.Status
	local playing = {}
	local playingTeam = game.Teams.Playing
	local playersingame = 0
	local murdingamechecked = {}
	local lobbyTeam = game.Teams.Lobby

	local murdermodule = require(game.ReplicatedStorage.MurderModule)

	inRound.Changed:Connect(function()

		if inRound.Value == true then

			for i, plr in pairs(game.Players:GetChildren()) do

				local char = plr.Character
				local humanRoot = char:WaitForChild("HumanoidRootPart")



				plr.Team = playingTeam

				char:WaitForChild("Humanoid").Died:Connect(function(playerwhodied)

					plr.Team = lobbyTeam
					table.remove(playing, playerwhodied)

				end)

			end	


		else

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

				plr.Team = lobbyTeam

				human:UnequipTools()
				for i, tool in pairs(plr.Backpack:GetChildren()) do

					if tool.Name == "6 gold chain guns" then

					else
						plr.Backpack:ClearAllChildren()
					end	
				end
			end
		end	
	end)



	local function round()	
		while true do

			local requiredPlayers = 2

			repeat
				wait(1)
				status.Value = "Atleast ".. requiredPlayers.." players are needed to start a round"

			until #game.Players:GetChildren() >= requiredPlayers

			inRound.Value = false


			for i = intermission, 0, -1 do

				status.Value = "Game will start in "..i.." seconds"

				wait(1)
			end
			inRound.Value = true
			for i, plr in pairs(game.Players:GetChildren()) do

				if plr.Team.Name == "Playing" then


					table.insert(playing, plr.Name)


				end	
			end
			playersingame = #game.Players:GetPlayers()
			murdermodule.ChooseRoles()
			for i = roundLength, 0, -1 do

				status.Value = "Game will end in "..i.." seconds"



				local murdereralive = false
				local nonmurderersalive = false
				local murderer
				print(murderer)
				for i, playerInRound in pairs(playing) do
					if game.Players:FindFirstChild(playerInRound) then
						if game.Players:FindFirstChild(playerInRound):WaitForChild("Role").Value == "Murderer" then
							murdereralive = true
							murderer = game.Players:FindFirstChild(playerInRound)
							print("murd is alive")
						elseif game.Players:FindFirstChild(playerInRound):WaitForChild("Role").Value ~= "Murderer" then
							nonmurderersalive = true
							print("innocents are alive")	
						end

					else
						if murderer == playerInRound then
							status.Value = "The Murderer has turned themselves in."
							if game.Workspace:FindFirstChild("GunClone") then
								game.Workspace.GunClone:Destroy()
								if game.Workspace:FindFirstChild("GunClone") then
									game.Workspace.GunClone:Destroy()
								end
							end
							wait(3)
							return scriptfunnyhaha()
						end
					end

				end

				if #playing == 0 then

					status.Value = "Everyone Has Died"
					if game.Workspace:FindFirstChild("GunClone") then
						game.Workspace.GunClone:Destroy()
						if game.Workspace:FindFirstChild("GunClone") then
							game.Workspace.GunClone:Destroy()
						end
					end
					wait(3)
					return scriptfunnyhaha()

				end
				print(tostring(murdereralive).. "murd")
				print(tostring(nonmurderersalive).. "nonmurd")
				if murdereralive == true and nonmurderersalive == false then
					status.Value = "The Murderer got away with their crimes."
					if game.Workspace:FindFirstChild("GunClone") then
						game.Workspace.GunClone:Destroy()
						if game.Workspace:FindFirstChild("GunClone") then
							game.Workspace.GunClone:Destroy()
						end
					end

					wait(3)
					return scriptfunnyhaha()
				end

				if murdereralive == false and nonmurderersalive == true then
					status.Value = "The Murderer has been rightfully killed!"
					if game.Workspace:FindFirstChild("GunClone") then
						game.Workspace.GunClone:Destroy()
						if game.Workspace:FindFirstChild("GunClone") then
							game.Workspace.GunClone:Destroy()
						end
					end
					wait(3)
					return scriptfunnyhaha()
				end

				if nonmurderersalive == true and i == 0 then
					status.Value = "The Murderer took too long and got caught."
					if game.Workspace:FindFirstChild("GunClone") then
						game.Workspace.GunClone:Destroy()
						if game.Workspace:FindFirstChild("GunClone") then
							game.Workspace.GunClone:Destroy()
						end
					end

					wait(3)
					return scriptfunnyhaha()
				end

				wait(1)
			end
			wait(3)
		end
	end



	spawn(round)
end
scriptfunnyhaha()

in the documentation Humanoid.Died doesn’t pass any parameter

how would i find out who died then?

with the plr from the for loop

can’t i just do this under everything?


local function onPlayerAdded(player)
	local function onCharacterAdded(character)
		local humanoid = character:WaitForChild("Humanoid")

		local function onDied()
			table.remove(playing, player)
		end

		humanoid.Died:Connect(onDied)
	end

	player.CharacterAdded:Connect(onCharacterAdded)
end

game.Players.PlayerAdded:Connect(onPlayerAdded)

(yes its stolen from the documentation)

you used “i” in both variable names for the for loops

you used i for both variables in for loops, maybe that was the problem

ah let me try to change that and ill report back to you

but i didn’t use the variables after that though.