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

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