How do I fix this?

Im trying to make a game loop, but the loop isn’t even working even though im the only person playing the game.

while true do
	print("while true")
	repeat
		requestChange:FireAllClients("Waiting For Players...")
		print("requestedIndexed")
		task.wait(2)
	until playerAmount >= 1
local Players = game:GetService("Players") local Rep = game:GetService("ReplicatedStorage") local Maps = game:GetService("ServerStorage"):WaitForChild("Maps"):GetChildren()
local playerAmount = #Players:GetPlayers()

That variable never changes since you never changed it when the player amount changes.
Switch your code with this:

while true do
	print("while true")
	repeat
        playerAmount = #Players:GetPlayers()
		requestChange:FireAllClients("Waiting For Players...")
		print("requestedIndexed")
		task.wait(2)
	until playerAmount >= 1
3 Likes

If this worked, please tell me.

It did, but now Im getting this error

  18:01:08.110  Unable to assign property Position. Vector3 expected, got Instance  -  Server - GameLoop:50
  18:01:08.110  Stack Begin  -  Studio
  18:01:08.110  Script 'ServerScriptService.GameLoop', Line 50  -  Studio - GameLoop:50
  18:01:08.111  Stack End  -  Studio
local Players = game:GetService("Players") local Rep = game:GetService("ReplicatedStorage") local Maps = game:GetService("ServerStorage"):WaitForChild("Maps"):GetChildren()
local playerAmount = #Players:GetPlayers()

local GameModules = script.Parent:WaitForChild("GameModules")

local TSM = require(script.Parent:WaitForChild("GameModules").troubleShoot)

local Remotes = Rep:WaitForChild("Remotes")

local requestChange = Remotes:FindFirstChild("requestChange")

local gameStarted = false

while true do
	print("while true")
	repeat
		playerAmount = #Players:GetPlayers()
		requestChange:FireAllClients("Waiting For Players...")
		print("requestedIndexed")
		task.wait(2)
	until playerAmount >= 1
	
	requestChange:FireAllClients("Intermission")
	
	task.wait(5)
	
	requestChange:FireAllClients("Picking Map...")
	local map = Maps[math.random(#Maps)]:Clone()
	
	task.wait(3)
	
	requestChange:FireAllClients("The Map "..tostring(map).." Has Been Chosen!")
	
	task.wait(4)
	
	requestChange:FireAllClients("Loading Game...")
	
	task.wait(5)
	
	local Spawns = map:WaitForChild("Spawns"):GetChildren()
	
	local Tags = 0

	for i, v in pairs(Players:GetChildren()) do
		for ii, vv in pairs(game.Workspace:GetChildren()) do
			if v.Name == vv.Name then
				local root = vv:FindFirstChild("HumanoidRootPart")
				
				if root then
					root.Position = Spawns[math.random(#Spawns)]
				else
					error("Root Cannot Be Found From "..v.."!")
				end
				
				local tag = Instance.new("BoolValue", vv)
				tag.Name = "inGame"
				Tags += 1
			end
		end
	end
	
	gameStarted = true
	
	local Time = 60
	
	requestChange:FireAllClients("You have "..Time.." Seconds!")
	
	local gameFinishedResult = " "
	
	repeat
		for i, v in pairs(game.Workspace:GetChildren()) do
			task.spawn(function()
				for ii, vv in pairs(Players:GetChildren()) do
					if vv.Name == v.Name then
						local humanoid = v:WaitForChild("Humanoid")

						if humanoid then
							humanoid.Died:Connect(function()
								local Tag = v:WaitForChild("inGame")
								
								if Tag then
									if Tag == true then
										Tag = false
									else
										return
									end
								elseif not Tag then
									TSM:troubleShoot(1)
								end
							end)
						elseif not humanoid then
							TSM:troubleShoot(2)
						end
					end
				end
			end)
		end
		
		task.wait(1)
		
		Time -= 1 requestChange:FireAllClients("You have "..Time.." Seconds!")
		
		if Tags == 1 then
			gameFinished = "onePlayer"
		elseif Tags == 0 then
			gameFinished = "noOneWon"
		else
			if Time == 0 then
				gameFinished = "timeout"
			end
		end
	until gameFinishedResult == "onePlayer" or gameFinishedResult == "noOneWon" or gameFinishedResult == "timeout"
end

Players.PlayerRemoving:Connect(function(player)
	local char = player.Character
	
	local tag = char:FindFirstChild("inGame")
	
	if char then
		if tag then
			tag = false
		elseif not tag or not char or not tag and char then
			TSM:troubleShoot(1)
		end
	end
end)

And the text isn’t even getting changed with the remote event, but no errors.

local Rep = game:GetService("ReplicatedStorage")
local Remotes = Rep:WaitForChild("Remotes")
local requestChange = Remotes:FindFirstChild("requestChange")

requestChange.OnClientEvent:Connect(function(player, text)
	script.Parent.Text = tostring(text)
end)

It’s because for OnClientEvent, you don’t need to include the player parameter. Try this instead for your client code:

local Rep = game:GetService("ReplicatedStorage")
local Remotes = Rep:WaitForChild("Remotes")
local requestChange = Remotes:FindFirstChild("requestChange")

requestChange.OnClientEvent:Connect(function(text)
	script.Parent.Text = tostring(text)
end)
2 Likes

Change your script to this: (you forgot to add a .Position)

local Players = game:GetService("Players") local Rep = game:GetService("ReplicatedStorage") local Maps = game:GetService("ServerStorage"):WaitForChild("Maps"):GetChildren()
local playerAmount = #Players:GetPlayers()

local GameModules = script.Parent:WaitForChild("GameModules")

local TSM = require(script.Parent:WaitForChild("GameModules").troubleShoot)

local Remotes = Rep:WaitForChild("Remotes")

local requestChange = Remotes:FindFirstChild("requestChange")

local gameStarted = false

while true do
	print("while true")
	repeat
		playerAmount = #Players:GetPlayers()
		requestChange:FireAllClients("Waiting For Players...")
		print("requestedIndexed")
		task.wait(2)
	until playerAmount >= 1

	requestChange:FireAllClients("Intermission")

	task.wait(5)

	requestChange:FireAllClients("Picking Map...")
	local map = Maps[math.random(#Maps)]:Clone()

	task.wait(3)

	requestChange:FireAllClients("The Map "..tostring(map).." Has Been Chosen!")

	task.wait(4)

	requestChange:FireAllClients("Loading Game...")

	task.wait(5)

	local Spawns = map:WaitForChild("Spawns"):GetChildren()

	local Tags = 0

	for i, v in pairs(Players:GetChildren()) do
		for ii, vv in pairs(workspace:GetChildren()) do
			if v.Name == vv.Name then
				local root = vv:FindFirstChild("HumanoidRootPart")

				if root then
					root.Position = Spawns[math.random(#Spawns)]:GetPivot().Position
				else
					error("Root Cannot Be Found From "..v.."!")
				end

				local tag = Instance.new("BoolValue", vv)
				tag.Name = "inGame"
				Tags += 1
			end
		end
	end

	gameStarted = true

	local Time = 60

	requestChange:FireAllClients("You have "..Time.." Seconds!")

	local gameFinishedResult = " "

	repeat
		for i, v in pairs(game.Workspace:GetChildren()) do
			task.spawn(function()
				for ii, vv in pairs(Players:GetChildren()) do
					if vv.Name == v.Name then
						local humanoid = v:WaitForChild("Humanoid")

						if humanoid then
							humanoid.Died:Connect(function()
								local Tag = v:WaitForChild("inGame")

								if Tag then
									if Tag == true then
										Tag = false
									else
										return
									end
								elseif not Tag then
									TSM:troubleShoot(1)
								end
							end)
						elseif not humanoid then
							TSM:troubleShoot(2)
						end
					end
				end
			end)
		end

		task.wait(1)

		Time -= 1 requestChange:FireAllClients("You have "..Time.." Seconds!")

		if Tags == 1 then
			gameFinished = "onePlayer"
		elseif Tags == 0 then
			gameFinished = "noOneWon"
		else
			if Time == 0 then
				gameFinished = "timeout"
			end
		end
	until gameFinishedResult == "onePlayer" or gameFinishedResult == "noOneWon" or gameFinishedResult == "timeout"
end

Players.PlayerRemoving:Connect(function(player)
	local char = player.Character

	local tag = char:FindFirstChild("inGame")

	if char then
		if tag then
			tag = false
		elseif not tag or not char or not tag and char then
			TSM:troubleShoot(1)
		end
	end
end)
1 Like

It works, but Im the only player left in the game with the only tag, so why doesn’t the game end automatically?

Try this:

--//Services
local Players = game:GetService("Players")
local ServerScriptService = game:GetService("ServerScriptService")
local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

--//Modules
local TSM = require(ServerScriptService:WaitForChild("GameModules").troubleShoot)

--//Variables
local GameModules = ServerScriptService:WaitForChild("GameModules")
local Remotes = ReplicatedStorage:WaitForChild("Remotes")
local requestChange = Remotes:FindFirstChild("requestChange")

--//Controls
local gameStarted = false
local playerAmount = #Players:GetPlayers()
local Maps = ServerStorage:WaitForChild("Maps"):GetChildren()
local Tags = 0

--//Functions
while true do
	print("while true")
	requestChange:FireAllClients("Waiting For Players...")
	Players.PlayerAdded:Wait()

	requestChange:FireAllClients("Intermission")

	task.wait(5)
	requestChange:FireAllClients("Picking Map...")
	
	local map = Maps[math.random(#Maps)]:Clone()

	task.wait(3)
	requestChange:FireAllClients("The Map "..tostring(map).." Has Been Chosen!")

	task.wait(4)
	requestChange:FireAllClients("Loading Game...")

	task.wait(5)
	local Spawns = map:WaitForChild("Spawns"):GetChildren()

	Tags = 0
	
	for i, player in ipairs(Players:GetPlayers()) do
		if not player.Character then
			continue
		end
		
		local tag = Instance.new("BoolValue")
		tag.Name = "inGame"
		tag.Value = true
		tag.Parent = player.Character
		
		Tags += 1
		
		local Humanoid = player.Character:FindFirstChildWhichIsA("Humanoid")
		
		if Humanoid then
			Humanoid.Died:Connect(function()
				if tag:IsDescendantOf(workspace) then
					tag.Value = false
					Tags -= 1
				else
					TSM:troubleShoot(1)
				end
			end)
		else
			TSM:troubleShoot(2)
		end
		
		local HumanoidRootPart = player.Character:FindFirstChild("HumanoidRootPart")
		
		if not HumanoidRootPart then
			warn(player.Name .."'s HumanoidRootPart was not found")
			
			continue
		end
		
		HumanoidRootPart.Position = Spawns[Random.new():NextInteger(1, #Spawns)]:GetPivot().Position
	end

	gameStarted = true

	local Time = 60

	requestChange:FireAllClients("You have "..Time.." Seconds!")

	local gameFinishedResult = nil
	
	while task.wait(1) and Time > 0 and not gameFinishedResult do
		Time -= 1
		
		requestChange:FireAllClients("You have "..Time.." Seconds!")

		if Tags == 1 then
			gameFinishedResult = "onePlayer"
		elseif Tags <= 0 then
			gameFinishedResult = "noOneWon"
		else
			if Time == 0 then
				gameFinishedResult = "timeout"
			end
		end
	end
	
	print(gameFinishedResult)
end

Players.PlayerRemoving:Connect(function(player)
	if not player.Character then
		return
	end
	
	local tag = player.Character and player.Character:FindFirstChild("inGame")
	
	if tag then
		tag = false
		Tags -= 1
	else
		TSM:troubleShoot(1)
	end
end)

I heavily optimized your script and fixed alot of things.

1 Like

It worked, but can you explain what you did different?

Also, I optimized it a bit more, but Im having another issue.

  19:48:40.896  ServerScriptService.GameLoop:114: attempt to index nil with 'Parent'  -  Server - GameLoop:114
  19:48:40.897  Stack Begin  -  Studio
  19:48:40.897  Script 'ServerScriptService.GameLoop', Line 114  -  Studio - GameLoop:114
  19:48:40.897  Stack End  -  Studio
--//Services
local Players = game:GetService("Players")
local ServerScriptService = game:GetService("ServerScriptService")
local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

--//Modules
local TSM = require(ServerScriptService:WaitForChild("GameModules").troubleShoot)

--//Variables
local GameModules = ServerScriptService:WaitForChild("GameModules")
local Remotes = ReplicatedStorage:WaitForChild("Remotes")
local requestChange = Remotes:FindFirstChild("requestChange")

--//Controls
local gameStarted = false
local playerAmount = #Players:GetPlayers()
local Maps = ServerStorage:WaitForChild("Maps"):GetChildren()
local Tags = 0

--//Functions
while true do
	print("while true")
	requestChange:FireAllClients("Waiting For Players...")
	Players.PlayerAdded:Wait()

	requestChange:FireAllClients("Intermission")

	task.wait(5)
	requestChange:FireAllClients("Picking Map...")

	local map = Maps[math.random(#Maps)]:Clone()

	task.wait(3)
	requestChange:FireAllClients("The Map "..tostring(map).." Has Been Chosen!")

	task.wait(4)
	requestChange:FireAllClients("Loading Game...")

	task.wait(5)
	local Spawns = map:WaitForChild("Spawns"):GetChildren()

	Tags = 0

	for i, player in ipairs(Players:GetPlayers()) do
		if not player.Character then
			continue
		end

		local tag = Instance.new("BoolValue")
		tag.Name = "inGame"
		tag.Value = true
		tag.Parent = player.Character

		Tags += 1

		local Humanoid = player.Character:FindFirstChildWhichIsA("Humanoid")

		if Humanoid then
			Humanoid.Died:Connect(function()
				if tag:IsDescendantOf(workspace) then
					tag.Value = false
					Tags -= 1
				else
					TSM:troubleShoot(1)
				end
			end)
		else
			TSM:troubleShoot(2)
		end

		local HumanoidRootPart = player.Character:FindFirstChild("HumanoidRootPart")

		if not HumanoidRootPart then
			warn(player.Name .."'s HumanoidRootPart was not found")

			continue
		end

		HumanoidRootPart.Position = Spawns[Random.new():NextInteger(1, #Spawns)]:GetPivot().Position
	end

	gameStarted = true

	local Time = 60

	requestChange:FireAllClients("You have "..Time.." Seconds!")

	local gameFinishedResult = nil

	while task.wait(1) and Time > 0 and not gameFinishedResult do
		Time -= 1

		requestChange:FireAllClients("You have "..Time.." Seconds!")

		if Tags == 1 then
			gameFinishedResult = "onePlayer"
		elseif Tags <= 0 then
			gameFinishedResult = "noOneWon"
		else
			if Time == 0 then
				gameFinishedResult = "timeout"
			end
		end
	end
	
	local winner = " "

	if gameFinishedResult == "onePlayer" then
		if playerAmount ~= 1 then
			for i, v in pairs(game.Workspace:GetChildren()) do
				local Tag = v:FindFirstChild("inGame")
				winner = Tag.Parent.Name
				
				requestChange(winner.." Has Won The Game!")
				
				if Tag then
					if Tag == true then
						for i, v in pairs(Players:GetChildren()) do
							if v.Name == winner then
								v:FindFirstChild("leaderstats").Wins.Value += 1
							end
						end
					end
				elseif not Tag then
					TSM:troubleShoot(1)
				end
			end
		else
			if playerAmount == 1 then
				requestChange("No one has won due to having one player in game...")
			end
		end
	elseif gameFinishedResult == "noOneWon" then
		requestChange("Everyone Died!")
	else
		if gameFinishedResult == "timeout" then
			requestChange("Players have ran out of time!")
		end
	end
	
	task.wait(7.5)
end

Players.PlayerRemoving:Connect(function(player)
	if not player.Character then
		return
	end

	local tag = player.Character and player.Character:FindFirstChild("inGame")

	if tag then
		tag = false
		Tags -= 1
	else
		TSM:troubleShoot(1)
	end
end)

Why is there a repeat until loop inside a while true loop, thats very bad practice and will greatly increase the lag, no need for the whole true loop to exist there.

There’s cooldowns though, they aren’t infinite, so would this still have a great impact on player ping?

And since you said that, do you have a better way of making this?

Not ping, by lag i mean fps and freezing games, it might affect the ping, not sure tho, just remove the while true loop, have the repeat until by itself

Would this freeze the game though?

Because the task.wait’s have throttling…

And whats the difference from having no while true loop but replacing it wilth a repeat?

The while true loop will run after it for no reason, you can check how much memory the script is taking, with this rate it would be a bit high

It isn’t, because its supposed to be a game loop.

So it basically goes back to intermission mode.

Oh alright then, still better to use a different type but your choice.

Thats why im asking…

1 Like

Have a function which runs at different times

local times = {
	10,
}

local funcs = {
	intermission = function()

	end,
}

while wait() do
	for i , v in pairs(times) do
		wait(v)
		funcs[i]() 
	end
end

Forgot to add the check for players, you can have it in the functions table inside the intermission tho.

1 Like

Why do you need to fire all clients anyway? Everything the server does will automatically replicate anyway.