This for loop is repeating inf for some reason?

I am trying to make a game roulette type of game

My error is one line 7 it is repeating inf

I have tried googling a similar error and adding prints

local Players = game:GetService("Players")
local RS, SS = game:GetService("ReplicatedStorage"), game:GetService("ServerStorage")
local function gamestart()
	print("Yes")
	if gamestart == false then
		print("yw4y4")
		for keyvalue, player in pairs(Players:GetPlayers()) do
			print("246246")
		local num = math.random(1,5)
		if workspace.Seats:FindFirstChild(num).Value.Value == "None" then
			workspace.Seats:FindFirstChild(num).Value.Value = player.Name
			workspace.Seats:FindFirstChild(num):Sit(player.Character.Humanoid)
			local PlayerNumberSeat = Instance.new("NumberValue")
			PlayerNumberSeat.Value = num
			PlayerNumberSeat.Parent = player.Character
			player.Character.PlayerInGame.Value = true
			print(player.Character.PlayerInGame.Value)
				workspace:WaitForChild("GameStarted").Value = true
				print("Yes")
				end
	
		end
	end
end
local function PlayerCountChecker()
	print("Yes")
	if #Players:GetPlayers() > 0 then
		RS.Intermisson:FireAllClients()
		task.delay(12, gamestart)
	else
		RS.NotEnoughPlayers:FireAllClients(1)
	end
end

Players.PlayerAdded:Connect(PlayerCountChecker)
Players.PlayerRemoving:Connect(PlayerCountChecker)

workspace.GameStarted.Changed:Connect(function()
	if workspace.GameStarted.Value == true then
		local RandomPlayer = Players:GetPlayers()[math.random(#Players:GetPlayers())]
		repeat until RandomPlayer.Character.PlayerInGame == true
		if RandomPlayer.Character.PlayerInGame == true then
		RS.PickPlayer:FireClient(RandomPlayer)
		local RandomPlayerWork = workspace:FindFirstChild(RandomPlayer.Name)
		local WeaponAim = SS.LocalLookingAroundScript:Clone()
		WeaponAim.Disabled = false
		WeaponAim.Parent = RandomPlayerWork
		print(RandomPlayer.Name)
			local Deagle = SS.Deagle:Clone()
			Deagle.Parent = RandomPlayerWork
			RS.PickPlayer:FireClient(RandomPlayer)
			print("Yes2")
------------------------------------------------------------------------------------------------------------
		RS.PickPlayer.OnServerEvent:Connect(function() 
				local SeatNumber = RandomPlayerWork.Value.Value + 1
				local playerSeat = workspace.Seats:FindFirstChild(SeatNumber)
			repeat until playerSeat.Occupant ~= nil  
			if playerSeat.Occupant ~= nil then
				local playerName = playerSeat.Value.Value
			local Nextplayer = workspace:FindFirstChild(playerName)
			local WeaponAim = SS.LocalLookingAroundScript:Clone()
			WeaponAim.Disabled = false
			WeaponAim.Parent = Nextplayer
			print(RandomPlayer.Name)
					Deagle.Parent = Nextplayer		
					RS.Fire.OnServerEvent:Connect(function()
						local bullet = game.ServerStorage.Bullet:Clone()	
						bullet.Parent = game.Workspace
						bullet.Position = Deagle.ShootPoint
						bullet.Orientation = Deagle.ShootPoint
						bullet.Touched:Connect(function(hit)
							hit.Parent:FindFirstChild("Humanoid").Health = hit.Parent:FindFirstChild("Humanoid") - math.random(99,100)
							bullet:Destroy()
							print("Yes3")
						end)
					end)
			end
		end)				
		end
		end
end)

Can you show me the output? It can help me

In this line that mean when player playing Your game ITS gonna print infinity until the player leave the game

What do you mean? For loops run only one time, you are misleading OP…

I believe the problem is with the workspace:WaitForChild("GameStarted").Value = true this keeps yielding the code per each player in the game.

But there is no reason for this to repeat infinitely, since there is no loop doing that.

OP should show the line where the function gets called.

I want it to get the players and run it for every player then stop

Your for loop isn’t repeating infinitely. I assume the code yields infinitely, probably because Workspace doesn’t have a child named “GameStarted”.

It’s useful to include exact copy and pasted error and warning messages.

Just noticed i forgot to define the if statement

1 Like

I was typing that right went u sent that lol
Thanks everyone for the help
Still getting the error
Script timeout: exhausted allowed execution time

1 Like

Heres the updated script

local Players = game:GetService("Players")
local RS, SS = game:GetService("ReplicatedStorage"), game:GetService("ServerStorage")
local function gamestart()
	print("Yes")
	print(game.Workspace.GameStarted.Value)
	if game.Workspace.GameStarted.Value == false then
		workspace:FindFirstChild("GameStarted").Value = true
		for keyvalue, player in pairs(Players:GetPlayers()) do
		local num = math.random(1,5)
		if workspace.Seats:FindFirstChild(num).Value.Value == "None" then
			workspace.Seats:FindFirstChild(num).Value.Value = player.Name
			workspace.Seats:FindFirstChild(num):Sit(player.Character.Humanoid)
			local PlayerNumberSeat = Instance.new("NumberValue")
			PlayerNumberSeat.Value = num
			PlayerNumberSeat.Parent = player.Character
			player.Character.PlayerInGame.Value = true
			print(player.Character.PlayerInGame.Value)
				end
	
		end
	end
end
local function PlayerCountChecker()
	print("Yes")
	if #Players:GetPlayers() > 0 then
		RS.Intermisson:FireAllClients()
		task.delay(12, gamestart)
	else
		RS.NotEnoughPlayers:FireAllClients(1)
	end
end

Players.PlayerAdded:Connect(PlayerCountChecker)
Players.PlayerRemoving:Connect(PlayerCountChecker)

workspace.GameStarted.Changed:Connect(function()
	if workspace.GameStarted.Value == true then
		local RandomPlayer = Players:GetPlayers()[math.random(#Players:GetPlayers())]
		repeat until RandomPlayer.Character:WaitForChild("PlayerInGame").Value == true
		if RandomPlayer.Character:WaitForChild("PlayerInGame").Value == true then
		RS.PickPlayer:FireClient(RandomPlayer)
		local RandomPlayerWork = workspace:FindFirstChild(RandomPlayer.Name)
		local WeaponAim = SS.LocalLookingAroundScript:Clone()
		WeaponAim.Disabled = false
		WeaponAim.Parent = RandomPlayerWork
		print(RandomPlayer.Name)
			local Deagle = SS.Deagle:Clone()
			Deagle.Parent = RandomPlayerWork
			RS.PickPlayer:FireClient(RandomPlayer)
			print("Yes2")
------------------------------------------------------------------------------------------------------------
		RS.PickPlayer.OnServerEvent:Connect(function() 
				local SeatNumber = RandomPlayerWork.Value.Value + 1
				local playerSeat = workspace.Seats:FindFirstChild(SeatNumber)
			repeat until playerSeat.Occupant ~= nil  
			if playerSeat.Occupant ~= nil then
				local playerName = playerSeat.Value.Value
			local Nextplayer = workspace:FindFirstChild(playerName)
			local WeaponAim = SS.LocalLookingAroundScript:Clone()
			WeaponAim.Disabled = false
			WeaponAim.Parent = Nextplayer
			print(RandomPlayer.Name)
					Deagle.Parent = Nextplayer		
					RS.Fire.OnServerEvent:Connect(function()
						local bullet = game.ServerStorage.Bullet:Clone()	
						bullet.Parent = game.Workspace
						bullet.Position = Deagle.ShootPoint
						bullet.Orientation = Deagle.ShootPoint
						bullet.Touched:Connect(function(hit)
							hit.Parent:FindFirstChild("Humanoid").Health = hit.Parent:FindFirstChild("Humanoid") - math.random(99,100)
							bullet:Destroy()
							print("Yes3")
						end)
					end)
			end
		end)				
		end
		end
end)