The winner is always the same player

Hello everyone, so as I was working on my game project when I stumbled on a problem. So there’s this issue where after the first round ends, starts a new round, then teleports the players, and if I reset one of the players it does not end the function and called the player still alive the winner. Instead, If I reset the player that’s still alive, the previous player that won the previous round would get the win. Why is that? I am very confused right now.

It occurs in this module function right here:

function moduleFunction.CheckWinner(parent)
	if #playersInRound > 1 then
		while #playersInRound >= 2 do
			parent.Value = "Game in progress"
			wait()
			if #playersInRound == 1 then
				local player = playersInRound[1]
				print("The Winner is: " .. player.Name)
				parent.Value = player.Name .. " has won this round!"
				wait(5)
			elseif #playersInRound == 0 then
				print("There was not a single winner.")
				parent.Value = "No one won this round"
				wait(5)
			end
		end
	end
end

I would appreciate it if some would help thanks.
If you would like to see the whole module please ask!

2 Likes

I dont know how to completely do something like this, but If you made a table or if you havent, you should make one and when a player dies or loses make it so they get removed from the table

Would you like to see the whole module? Yes there are tables that I use to run the code.

I guess, Im a begginer in scripting still, but I can try to help you out

You can make a room that goes around the player and they are forced to reset.

local moduleFunction = {}
local playersInRound = {}
local connections = {}

local playerInfo = {
	AFK = false; 
	InRound = false
}

function moduleFunction.ReturnPlayersInfo()
	return playerInfo
end
---------------------------------------------------------------------------------------------------------------------------
-- // Game Functions \\ --
---------------------------------------------------------------------------------------------------------------------------
function moduleFunction.AddValue()
	local Players = game:GetService("Players")
	Players.PlayerAdded:Connect(function(player)

		local isPlaying  = Instance.new("BoolValue")
		isPlaying .Name = "IsPlaying" 
		isPlaying .Parent = player 

	end)
end

function moduleFunction.AddPlayer(player)
	if player:IsA("Player") then
		if playerInfo.AFK == false then
			playerInfo.InRound = true
			print(player)
			table.insert(playersInRound, player) 
			connections[player.Name] = player.Character.Humanoid.Died:Connect(function()
				table.remove(playersInRound, table.find(playersInRound, player))
			end)
		elseif playerInfo.AFK == true then
			warn("Player is afk. Was not added to the list")
			playerInfo.InRound = false
		end	
	end
	for i, player in pairs (playersInRound) do
		print(player)
		wait()
	end
end

function moduleFunction.PlayerLeft()
	connections["Removing"] = game.Players.PlayerRemoving:Connect(function(player) 
		local playerLeft = table.find(playersInRound, player)
		if playerLeft then
			table.remove(playersInRound, playerLeft)
			playerInfo.InRound = false
		end
	end)
end

function moduleFunction.CheckWinner(parent)
	if #playersInRound > 1 then
		while #playersInRound >= 2 do
			parent.Value = "Game in progress"
			wait()
			if #playersInRound == 1 then
				local player = playersInRound[1]
				print("The Winner is: " .. player.Name)
				parent.Value = player.Name .. " has won this round!"
				wait(5)
			elseif #playersInRound == 0 then
				print("There was not a single winner.")
				parent.Value = "No one won this round"
				wait(5)
			end
		end
	end
end

function moduleFunction.RemovePlayer()
	for _, connection in pairs(connections) do 
		connection:Disconnect()
		wait()
	end

	for _, player in pairs(playersInRound)do
		player:LoadCharacter()
		print(player)
		wait()
	end	
	
	playerInfo.InRound = false

end 

return moduleFunction

Ignore the first function, I didn’t use it in any scripts

1 Like

That’s not the exact issue tho. The issue is that whoever wins the first round for some reason wins all the rounds right after that even if that player died.

You can also reset the data from the previous round.

Didn’t I do it in this function here?

Yeah you did. Have you tested it yet?

Yes I did I have been trying to tell you its not working.

Would you like to see the game script that’s running most of the functions of the module?

I would recommend going on to YouTube and going into this problem further also you can go to the studio bugs section and submit a bug report.

I don’t really think its a bug. And whenever I report bugs I always get a flagged for not a reason and no help.

Would you like to take a glance at the main script?

Have you tried looking it up on YouTube or google?

Sure I would like to have a look at the full script and analyse it.

Code has been private sorry

This is the game script that runs the module functions, etc.
The module was on the pervious posts.

Please try to look up this issue on google or YouTube.

Yes I tired searching this up and found nothing related to my problem.