Game Loop And Remote Events do not work

These Scripts Don’t Work, I’m trying to make a disaster game but the issue is that these scripts wont work at all, they just error.

Error

  12:24:20.318  RBLXDisasters @ 19 Aug 2021 12:24 auto-recovery file was created  -  Studio - C:/Users/mrtix/OneDrive/Documents/ROBLOX/AutoSaves
  12:24:22.281  OnClientEvent can only be used on the client  -  Server - ShowSurvivorScript:5
  12:24:22.282  Stack Begin  -  Studio
  12:24:22.282  Script 'ServerScriptService.ShowSurvivorScript', Line 5  -  Studio - ShowSurvivorScript:5
  12:24:22.282  Stack End  -  Studio
  12:24:22.282  ServerScriptService.GameLoop:101: Expected ',' when parsing index range, got 'in'  -  Studio - GameLoop:101

Game Loop

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game.Players:GetPlayers()
local ShowSurvivorEvent = ReplicatedStorage:WaitForChild("Events"):WaitForChild("ShowSurvivorEvent")
local ServerStorage = game:GetService("ServerStorage")
local MapsFolder = ServerStorage:WaitForChild("Maps")
local GameStatisticsGui = game.StarterGui.GameStatisticsGui
local SurvivorList = GameStatisticsGui:WaitForChild("SurvivorsFrame")
local WinReward = 1
local CoinReward = 25
local Status = ReplicatedStorage:WaitForChild("Values"):WaitForChild("Status")
local GameLength = 120
-- Game Loop
while true do

	Status.Value = "Waiting For 2 Players..."
	repeat wait() until game.Players.NumPlayers >= 1
	Status.Value = "Intermission..."
	wait(20)

	local plrs = {}
	for i, player in pairs(game.Players:GetPlayers()) do
		if player then
			table.insert(plrs,player) -- Adding Players
		end
	end

	wait(2)

	local AvaliableMaps = MapsFolder:GetChildren()
	local ChosenMap = AvaliableMaps[math.random(1, #AvaliableMaps)]

	Status.Value = "Map Chosen!"

	local ClonedMap = ChosenMap:Clone()
	ClonedMap.Parent = workspace

	-- Teleport Map
	local SpawnPoints = ClonedMap:FindFirstChild("SpawnPoints")
	if not SpawnPoints then
		warn("SpawnPoints Can't Be Found! Check if The Grouped Map Has Them!")
	end

	local AvaliableSpawnPoints = SpawnPoints:GetChildren()

	for i, player in pairs(plrs) do
		if player then
			character = player.Character

			if character then
				-- Teleport Them

				character:FindFirstChild("HumanoidRootPart").CFrame = AvaliableSpawnPoints[1].CFrame
				table.remove(AvaliableSpawnPoints,1)

				-- Game Tag
				local GameTag = Instance.new("BoolValue")
				GameTag.Name = "GameTag"
				GameTag.Parent = player.Character
			else
				warn("Player Can't Be Found!")
				if not player then
					table.remove(plrs,i)
				end
			end
		end
	end

	wait(2)
	Status.Value = "The Game Is Starting!"

	for i = GameLength,0,-1 do
		for x, player in pairs(plrs) do
			if player then
				character = player.Character
				if not character then
					-- Player Left
				else
					if character:FindFirstChild("GameTag") then
						-- Their Alive
					else
						-- Their Dead
						table.remove(plrs,x)
					end
				end
			else
				table.remove(plrs,x)
			end
		end

		Status.Value = "Time: "..i
		wait(1)

		if GameLength == 0 then
			for i, player in pairs(#plrs) do
				if player then
					for i, player in pairs(plrs) do
						if player then
							for i, GameStatisticsGui in pairs(plrs) do
								local GameStatisticsTextLabels = GameStatisticsGui.SurvivorsFrame:GetChildren()

								for i = 1 + plrs in pairs(#plrs) do
									GameStatisticsTextLabels[i] = plrs.Name
									if #plrs == 0 then
										Status.Value = "Nobody Won!"
									end
								end
							end
						end
					end
					character = player.Character

					if character then
					end
				end
			end
		end

		Status.Value = "End of game!"

		for i, player in pairs(game.Players:GetPlayers()) do
			character = player.Character
			if not character then
				-- Ignore
			else
				if character:FindFirstChild("GameTag") then
					character.GameTag:Destroy()
				end
			end
			player:LoadCharacter()
		end
	end
	ClonedMap:Destroy()
end

Show Survivors Script

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ShowSurvivorEvent = ReplicatedStorage:WaitForChild("Events"):WaitForChild("ShowSurvivorEvent")
local StarterGui = game:GetService("StarterGui")

ShowSurvivorEvent.OnClientEvent:Connect(function()
	StarterGui:WaitForChild("GameStatisticsGui").SurvivorsFrame.Visible = true
	StarterGui:WaitForChild("GameStatisticsGui").TitleLabel.Visible = true
	wait(10)
	StarterGui:WaitForChild("GameStatisticsGui").SurvivorsFrame.Visible = false
	StarterGui:WaitForChild("GameStatisticsGui").TitleLabel.Visible = false
end)

What kind of script is your Show Survivors script? (Script, ModuleScript (If so what is requiring it), LocalScript), and where is it parented to?

The reason your script is erroring is because it is running on a server script hence the error:
OnClientEvent can only be used on the client

You can fix this by ensuring that the script is running on a LocalScript on the client

1 Like

I assume this is the line that is erroring.
Did you mean to do

for i = 1 + #plrs do

or

for i, plr in pairs(#plrs) do

Is a Script, a Regular script, And It is parent is ServerScriptService

So would I make it a local Script?

If you’re attempting to manipulate a player’s Gui then it would have to be a LocalScript

I suggest making a LocalScript and parenting it to the Gui you are working with and then refering to the gui as:

local Gui = script.Parent
1 Like

Hello, could you explain what exactly you’re attempting to do in this block of code?
I know it contains your problem but I am not sure how to fix it as it is rather hard to tell what it’s meant to do.

If the game length is 0 then the round ended, I get this thing cald the GameStatistics the Frame, Then I make it so that for every player who is still in the game, Their name will get put on one of the TextLabels for GameStatistics.

You are not able to modify GUI from the server side.

1 Like

Then how else would I make the game loop run and the UI Pop up?

You will have to make use remoteevents, telling the client to do it when the event is fired from a local script