Changing is bool value is giving me and error?

So im making a game and for some reason changing this bool value is breaking my script(line 3)
The error is from line 7 however when I remove line 3 it works( Script timeout: exhausted allowed execution time)
Thanks in advance

local Players = game:GetService("Players")
local GameStartedValue = game.Workspace.GameStarted
local RS, SS = game:GetService("ReplicatedStorage"), game:GetService("ServerStorage")
local function gamestart()
	if game.Workspace.GameStarted.Value == false then
		GameStartedValue.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:FindFirstChild("PlayerInGame").Value = true
				end
	
		end
	end
end
local function PlayerCountChecker()
	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)
			wait(2)
------------------------------------------------------------------------------------------------------------
		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)

Whats the error in the output saying?

The error says this ^
Thrown in line 7

1 Like

whoops. thanks idkw I didn’t just LOOK.

I’d say add a couple wait()'s and see what it does, I would assume the reason is because you’re requesting too many things at once, this will then cause script exhaustion.

I added some waits and it didnt work

You’re also defining two things with one local, add another local statement to define either RS, or SS.

try adding a wait to this line:

repeat wait() until RandomPlayer.Character:WaitForChild("PlayerInGame").Value == true

Both of those fixes didn’t work

1 strange thing I never seen people put variables like you did on line 3 you did Rs and ss I think to make this code more not complicated you should put them as regular variables…

Put this in output bar.

settings().Studio.ScriptTimeoutLength = -1

For reference, the error message is coming from your code running to long without moving onto the next cycle. It often comes from loops that don’t end and don’t yield.

I’m not sure why a for loop is giving you this error. I’d recommend swithcing pairs to ipairs, since GetPlayers/GetChildren return arrays, which ipairs is meant for.

Edit:
Wait what line is giving you the error? It’s probably coming from your repeat until loop. If you don’t do repeat task.wait() until it basically does it an inf number of times (or it tries to until that kills your program).

There was two repeat statements I had and adding waits to both of them worked thank you!