I am getting error about nil value which I can't find

I am getting error about “nil”, I don’t know why

Annotation%202019-09-29%20114321

wait()

local Players = game:GetService("Players")
local Physics = game:GetService("PhysicsService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Workspace = game:GetService("Workspace")
local SoundService = game:GetService("SoundService")
local Lighting = game:GetService("Lighting")

local Settings = ReplicatedStorage:WaitForChild("Settings")

local RoleGhost = Settings:WaitForChild("RoleGhost").Value
local RoleSurvivor = Settings:WaitForChild("RoleSurvivor").Value
local RoleNone = Settings:WaitForChild("RoleNone").Value

local LobbySpawn = Workspace:WaitForChild("LobbySpawnLocation")
local LobbySurfaceGui = Workspace:WaitForChild("SignBox"):WaitForChild("SurfaceGui")

local CurrentGhost = nil

local InMapPlayers = {}
local InProgressGame = false

local RemoteEvents = ReplicatedStorage:WaitForChild("RemoteEvents")
local RoleChoosenEvent = RemoteEvents:WaitForChild("RoleChoosen")
local Results = RemoteEvents:WaitForChild("Results")

Lighting.FogStart = 20
Lighting.FogEnd = 25
Lighting.Brightness = 1

local Results = {
	Role = "",
	Player = ""
}

Players.PlayerRemoving:Connect(function(Player)
	if Player then
		for i, v in pairs(InMapPlayers) do
			if v == Player.Name then
				table.remove(InMapPlayers, i)
				break
			end
		end
	end
end)

function createResults()
	for i, v in pairs(Players:GetChildren()) do
		if v then
			Results:FireAllClients(v, "", "")
			if Results.Role then
				Results:FireAllClients(v, Results.Role, "")
				if Results.Player then
					Results:FireAllClients(v, Results.Role, Results.Player)
				end
			end
		end
	end
end

function setSurvivorPlayer(Player)
	for i, v in pairs(Workspace:WaitForChild(Player.Name):GetChildren()) do
		if v:IsA("Part") or v:IsA("MeshPart") then
			if v.Transparency then
				if v.Name == "HumanoidRootPart" then
					
				else	
					v.Transparency = 0
				end
			end
			if v.Material then
				v.Material = Enum.Material.SmoothPlastic
			end
		end
	end
end

function setGhostPlayer(Player)
	for i, v in pairs(Workspace:WaitForChild(Player.Name):GetChildren()) do
		if v:IsA("Part") or v:IsA("MeshPart") then
			if v.Transparency then
				if v.Name == "HumanoidRootPart" then
					
				else
					v.Transparency = 0.5
				end
			end
			if v.Material then
				v.Material = Enum.Material.ForceField
			end
		end
	end
end

function teleportPlayersToLobby()
	for i, v in pairs(InMapPlayers) do
		if Workspace:FindFirstChild(v) then
			if Workspace[v]:FindFirstChild("HumanoidRootPart") then
				if Workspace[v].HumanoidRootPart.Position then
					Workspace[v].HumanoidRootPart.Position = LobbySpawn.Position + Vector3.new(0, 3, 0)
					if Workspace[v]:FindFirstChild("Role") then
						if Workspace[v].Role.Value then
							Workspace[v].Role.Value = RoleNone
							setSurvivorPlayer(Players:WaitForChild(v))
						end
					end
				end
			end
		end
	end
	InProgressGame = false
	InMapPlayers = {}
end

function gameover()
	Results.Role = RoleSurvivor
	Results.Player = ""
	createResults()
	teleportPlayersToLobby()
end

function checkIfGameIsOver()
	if #InMapPlayers <= 1 then
		if CurrentGhost then
			for i, v in pairs(InMapPlayers) do
				if v == CurrentGhost then
					Results.Role = RoleGhost
					Results.Player = CurrentGhost
					break
				end
			end
		end
		createResults()
		teleportPlayersToLobby()
	end
end

function checkIfGhostHitSurvivor()
	local Group = Workspace:GetChildren()
	for i, v in pairs(Group) do
		if Players:FindFirstChild(v.Name) then
			if v:FindFirstChild("Role") then
				if v.Role.Value then
					if v.Role.Value == "Survivor" then
						if CurrentGhost then
							if (v.HumanoidRootPart.Position - game.Workspace:WaitForChild(CurrentGhost.Name):WaitForChild("HumanoidRootPart").Position).magnitude < 10 then
								v.Humanoid:TakeDamage(100)
								if v then
									for TableNumber, TableObject in pairs(InMapPlayers) do
										if TableObject == v.Name then
											table.remove(InMapPlayers, TableNumber)
											break
										end
									end
								end
							else
								-- Do not
							end
						end
					end
				end
			end
		end
	end
end

function teleportPlayerToMap(Player)
	local Group = Workspace:WaitForChild("SpawnLocations"):GetChildren()
	local Choosen = math.random(1, #Group)
	for i, v in pairs(Group) do
		if Choosen == i then
			if Workspace:FindFirstChild(Player.Name) then
				if Workspace[Player.Name]:FindFirstChild("HumanoidRootPart") then
					if Workspace[Player.Name].HumanoidRootPart.Position then
						Workspace[Player.Name].HumanoidRootPart.Position = v.Position + Vector3.new(0, 3, 0)
						table.insert(InMapPlayers, Player.Name)
						break
					end
				end
			end
		end
	end
end

function setRolesForPlayers()
	local Choosen = 0
	local Group = Players:GetChildren()
	Choosen = Group[math.random(1, #Group)]
	
	-- Clear all roles
	CurrentGhost = nil
	for i, v in pairs(Group) do
		if Workspace:FindFirstChild(v.Name) then
			if Workspace[v.Name]:FindFirstChild("Role") then
				if Workspace[v.Name].Role.Value then
					Workspace[v.Name].Role.Value = RoleNone
					setSurvivorPlayer(Players:WaitForChild(v.Name))
				end
			end
		end
	end
	
	-- Set ghost role
	if Choosen.Character then
		if Choosen.Character:FindFirstChild("Role") then
			if Choosen.Character.Role.Value ~= RoleGhost then
				Choosen.Character.Role.Value = RoleGhost
				setGhostPlayer(Players:WaitForChild(Choosen.Name))
				teleportPlayerToMap(Players:WaitForChild(Choosen.Name))
				CurrentGhost = Players:WaitForChild(Choosen.Name)
				local Player = Players:WaitForChild(Choosen.Name)
				RoleChoosenEvent:FireAllClients(Player, RoleGhost)
				print(Choosen.Name.. "is now a ".. RoleGhost)
			end
		end
	end
	
	-- Set survivor role
	for i, v in pairs(Group) do
		if Workspace:FindFirstChild(v.Name) then
			if Workspace[v.Name]:FindFirstChild("Role") then
				if Workspace[v.Name].Role.Value then
					if (Workspace[v.Name].Role.Value == RoleNone or RoleSurvivor) and (Workspace[v.Name].Role.Value ~= RoleGhost) then
						Workspace[v.Name].Role.Value = RoleSurvivor
						setSurvivorPlayer(Players:WaitForChild(v.Name))
						teleportPlayerToMap(Players:WaitForChild(v.Name))
						local Player = Players:WaitForChild(v.Name)
						RoleChoosenEvent:FireAllClients(Player, RoleSurvivor)
						print(v.Name.. "is now a ".. RoleSurvivor)
					end
				end
			end
		end
	end
end

spawn(function()
	local db = false
	while wait() do
		if InProgressGame == true then
			LobbySurfaceGui.TextLabel.Text = "Game In Progress..."
			spawn(function()
				local db = false
				while wait() do
					if db == false then
						db = true
						wait(2)
						checkIfGameIsOver()
						db = false
					end
				end
			end)
		else
			local Group = Players:GetChildren()
			if #Group >= 2 then
				LobbySurfaceGui.TextLabel.Text = "Starting game..."
				if db == false then
					db = true
					wait(25)
					setRolesForPlayers()
					InProgressGame = true
					db = false
				end
			else
				LobbySurfaceGui.TextLabel.Text = "Waiting For Players..."
			end
		end
		checkIfGhostHitSurvivor()
		print(#InMapPlayers)
	end
end)

while wait(235) do
	if InProgressGame == true then
		InProgressGame = false
		gameover()
	else
		local Group = Players:GetChildren()
		if #Group >= 2 then
			setRolesForPlayers()
			InProgressGame = true
		end
	end
end

First of all, in some functions, you are directly setting the values Role and Player, instead of setting the .Value properties of those objects.

Second of all, in createResults, you are firing to ALL CLIENTS for EVERY CLIENT.

Finally, You overwrote your Results RemoteEvent variable with the table Results = {Role = "",Player = ""}.

2 Likes

Yes, I am firing every client, since I want to show results for every player, secondly what do you mean by this what you wrote. What I should change, or improve here to fix nil value error?

You can just call Results:FireAllClients once, and that would broadcast to all players:

function createResults()
	Results:FireAllClients(Results.Role or "", Results.Player or "")
end
2 Likes

Well, if you want to show every player’s results, you should just clump all of the data together to make it cleaner. Additionally, you are potentially firing 3 times per player, which is very redundant.

(What goldenstein64 said)

You should change your Results variable to just be the RemoteEvent.

1 Like

I fixed the 3 times firing, it was just for test

1 Like

I see, I am dump, I put fire all to for loop xD

1 Like