Game loop not functioning properly

I am trying to make a game where a player chooses another player, who chooses another player, and so on (each time a player gets randomly damaged), but the script is not working. No errors are returned.

Part of script that is erroring

(My code is a mess, sorry)

function module:startGame() -- no parameters
	local playersInGame = {}
	local attachDetach = require(workspace.Platforms.attachModule)
	module:setServerText("<b>Game is starting...</b>")
	task.wait(4)
	for _, player in pairs(game.Players:GetPlayers()) do
		require(player.PlayerGui.GameUI.Transition.TransitionManager):transition(1)
	end
	task.wait()
	for i, player in pairs(game.Players:GetPlayers()) do
		local platformNum = nil
		local indexPlat = nil
		if i < 8 then
			platformNum = "P" ..i
			attachDetach:attach(player,platformNum)
			indexPlat = i
		else
			warn("error lol - too many players - please go die - looping around platforms")
			platformNum = "P" ..i-8
			attachDetach:attach(player,platformNum)
			indexPlat = i-8
		end
		table.insert(playersInGame,{player,indexPlat})
	end
	module:setServerText("<b>Welcome.</b>")
	local chosen = module:chooseRandom(playersInGame)
	local playerTable = playersInGame[chosen]
	local name, platform = table.unpack(playerTable)
	task.wait(2)
	module:setServerText("<b>The first person is</b> " ..name.Name .." <b>(Platform</b> " ..platform .."<b>)</b>")
	task.wait(3)
	local nextUp = chosen
    local round = 1
	while #playersInGame > 0 do
		local nextUpName = nil
		local nextUpPlat = nil
		if nextUp == "gameChooseRandom" then
			table.remove(playersInGame,table.find(playersInGame,playerTable))
			module:setServerText("<b>The chooser left the game. A random person will be picked to go next.</b>")
			local nextUpNum = module:chooseRandom(playersInGame)
			playerTable = playersInGame[nextUpNum]
			name, platform = table.unpack(playerTable)
			nextUpName = name.Name
			nextUpPlat = platform
			nextUp = nextUpNum
		elseif nextUp ~= nil and nextUp ~= "gameChooseRandom" then
			local found = nil
			for _,entry in pairs(playersInGame) do
				local plrObj,plrPla = table.unpack(entry)
				if plrObj.Name == nextUp then
					found = entry
					nextUpPlat = plrPla
				end
			end
			nextUpName = nextUp
			nextUp = table.find(playersInGame,found)
		else
			module:panic()
		end
		if nextUpName and nextUpPlat then
			module:setServerText("<b>Next up is</b> " ..nextUpName .." <b>(Platform</b> " ..nextUpPlat .."<b>)</b>")
		end
        if round == 1 then
            nextUp = chosen
        end
		local roundComplete = module:runRound(nextUp,playersInGame)
		repeat task.wait() until roundComplete
		nextUp = roundComplete
        round += 1
	end
	module:endGame()
end

function module:panic()
	for _,plr in pairs(game.Players:GetPlayers()) do
		plr:Kick("The server encountered a critical error and requires a shutdown. Please join a new server.")
	end
end

function module:runRound(chosen,playersInGame)
	local playerTable = playersInGame[chosen]
	local name, platform = table.unpack(playerTable)
	module:setServerText(name.Name .." <b>is choosing who to damage.</b>")
	if name then
		name.PlayerGui.GameUI.ChoosePlayer:TweenPosition(UDim2.new(0,0,1,0),Enum.EasingDirection.InOut,Enum.EasingStyle.Quad,0.5,true)
		chosen = require(name.PlayerGui.GameUI.ChoosePlayer.LoadPlayers):load(playersInGame)
		repeat task.wait() until chosen
		name.PlayerGui.GameUI.ChoosePlayer:TweenPosition(UDim2.new(0,0,1.8,0),Enum.EasingDirection.InOut,Enum.EasingStyle.Quad,0.5,true)
	else
		chosen = nil
	end
	if chosen then
		if chosen == "random_!Choice" then
			local newRand = module:chooseRandom(playersInGame)
			local randPlayer = playersInGame[1]
			local name, platform = table.unpack(randPlayer)
			chosen = name.Name
		end
		module:setServerText(name.Name .." <b>chose for</b> " ..chosen .." <b>to take damage.</b>")
		task.wait(5)
		local result = math.random(0,10)
		spinner:FireAllClients(workspace.RollRoom.Spinner.SpinScript,result)
		task.wait(3)
		module:setServerText(chosen .." <b>lost</b> " ..result .." <b>health.</b>")
		print('fired')
		game.Players:FindFirstChild(chosen).Character.Humanoid.Health -= result
		wait(3)
		if game.Players:FindFirstChild(chosen).Character.Humanoid.Health <= 0 then
			table.remove(playersInGame,table.find(playersInGame,playerTable))
			module:setServerText(chosen .." <b>has died.</b>")
		end
	elseif chosen == nil then
		chosen = "gameChooseRandom"
	end
	return chosen
end

When I go into testing and start the game, it allows the first player to choose, but after that it continues looping on the same player until the game ends.

All help is appreciated!

I solved this issue. A script in the player’s PlayerGui didn’t reset the ChosenPlayer value after returning it.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.