Party System ModuleScript error

I am getting an error at the three:

table.remove(PartyGroups, table.find(PartyGroups, self))

Here is the whole script:

-- Lugical
local PlayerService = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TelerportService = game:GetService("TeleportService")
local updatePartyEvent = ReplicatedStorage.UpdatePartyStatus
local universalIssueID = "Y1MzItBfyU"
local mainPlaceID = {["Urban Falls"] = 5041125493, ["East Bay Port"] = 5119882137}
local Party = {}
local PartyGroups = {}
Party.__index  = Party

--CONFIGURATIONS COMPONENT ORDER
--map
--public status
--rank requirement
--difficulty
--leader
--people

--Party Module
--Create the system where party groups are formed, and can be dealt with accordingly
function Party.new(configurations, play) -- Create a new party group
	local newPartyFormed = {} -- Metatable Work
	setmetatable(newPartyFormed, Party)
	table.insert(PartyGroups, newPartyFormed)
	
	newPartyFormed.Map = configurations.Map -- Attach the party settings to the party group
	newPartyFormed.PublicStatus = configurations.PublicStatus
	newPartyFormed.RankRequirement = configurations.RankRequirement
	newPartyFormed.Difficulty = configurations.Difficulty
	newPartyFormed.PartyLeader = play
	newPartyFormed.People = configurations.People
	
	return newPartyFormed, PartyGroups -- Send the new party group to the server
end

function Party.RetrieveUniversalId()
	return universalIssueID
end

function Party:Remove(yieldForPlayerExit, forceShutdown) -- Get rid of a party group. Optional argument to wait for all players to leave and to shutdown regardless.
	if self.People then -- Checks if people values are there.
		if not forceShutdown then
			local canRemove = true -- Sees if players are out/party is ready to be removed
			for index, player in pairs(self.People) do
				if player then
					if yieldForPlayerExit then
						local found = false
						PlayerService.PlayerRemoving:Connect(function(plr)
							if plr == player then
								found = true
							end
						end)
						while not found do
							PlayerService.PlayerRemoving:Wait()
							game:GetService("RunService").Heartbeat:Wait()
						end
						if index >= #self.People then
							canRemove= true
						end
					else
						canRemove = false
					end
				end
			end
		
			if canRemove then  -- Sends back whether party can be removed
				ReplicatedStorage.PartyChange:FireAllClients(false, tostring(self), self)
				if workspace:FindFirstChild("PARTY: "..tostring(self)) then
					wait(1)
						workspace:FindFirstChild("PARTY: "..tostring(self)):Destroy()
				end
				table.remove(PartyGroups, table.find(PartyGroups, self))
				self = {}
				return true, PartyGroups
			else
				return false, PartyGroups
			end
		else
			for _, v in pairs(self.People) do
				if v and v ~= self.PartyLeader then
				ReplicatedStorage.PartySignal:FireClient(v, nil, "Shutdown")
				end
			end
			ReplicatedStorage.PartyChange:FireAllClients(false, tostring(self), self)
			if workspace:FindFirstChild("PARTY: "..tostring(self)) then
					workspace:FindFirstChild("PARTY: "..tostring(self)):Destroy()
				end
			table.remove(PartyGroups, table.find(PartyGroups, self))
			self = {}
			return true, PartyGroups
		end
	else
		ReplicatedStorage.PartyChange:FireAllClients(false, tostring(self), self)
		table.remove(PartyGroups, table.find(PartyGroups, self))
		self = {}
		return true, PartyGroups
	end
end

function Party:JoinRequest(requestingPlayer) -- Handles player requests
	if self.PublicStatus then -- If public server, automatically handle the joining process
		if not table.find(self.People, requestingPlayer) then 
			print()
			if #self.People < 6 then
				table.insert(self.People, requestingPlayer)
				ReplicatedStorage.UpdatePartyStatus:FireAllClients(self, tostring(self))
				ReplicatedStorage.EventSignal:FireAllClients(requestingPlayer)
				return true, PartyGroups
			else
				return false, PartyGroups, "FullParty"
			end
		else
			return false, PartyGroups, "NoIssue"
		end
	else -- If private, handle it with owner approval first
		local grantedStatus = ReplicatedStorage.PlayerRequest:InvokeClient(self.PartyLeader, requestingPlayer)
		print("GSIUF", grantedStatus)
		if grantedStatus and grantedStatus == requestingPlayer and #self.People < 6 then
			local inAnotherParty = false
			for pos, party in pairs(PartyGroups) do
				for _, player in pairs(party) do
					if player then
						if player == requestingPlayer then
							inAnotherParty = true
						end
					else
						table.remove(party, pos)
					end
				end
			end
			if not inAnotherParty then
				table.insert(self.People, #self.People +1, requestingPlayer)
				updatePartyEvent:FireAllClients(self, tostring(self))
				ReplicatedStorage.EventSignal:FireAllClients(requestingPlayer)
				return true, PartyGroups
			end
		else
			return false, PartyGroups, "NoIssue"
		end
	end
end

function Party:RemovePlayer(player)
	if player == self.PartyLeader then
		self:Remove(false, true)
	else
	for position, person in pairs(self.People) do
		if person == player then
			table.remove(self.People, position)
				updatePartyEvent:FireAllClients(self)
		end
	end
	end
	return PartyGroups
end

function Party:Teleport()
	if self.People then
		local people = {}
		for i, v in pairs(self.People) do
			if v then
				table.insert(people, v)
			else
				table.remove(self.People, i)
			end
		end
		pcall(function()
		local reservedServerKey = TelerportService:ReserveServer(mainPlaceID[self.Map])
		TelerportService:TeleportToPrivateServer(mainPlaceID[self.Map], reservedServerKey, people)
		end)
	end
end
return Party

Does anyone have a fix?

Hey!
As the person who originally wrote this script (I know what you’re using it for, good luck by the way!), what’s happening occasionally, I believe is that the ModuleScript is attempting to remove a party from its table of currently running parties, when it may have already been removed prior. To circumvent an error, you could do:

if table.find(PartyGroups, self) then --> Verify the party is in the table first.
     table.remove(PartyGroups, table.find(PartyGroups, self))
end

However, I’m not entirely sure if it would fix the whole issue. Let me know if anything happens!

Now im getting this at the bottom (in white circle):

It seems you’re missing an end statement somewhere to close a function.

Yeah, ive put one everywhere around the lines states but it would always say it was in the wrong place. Could you help me find where its supposed to go as I cannot find it?

Updated Script:

-- Lugical
local PlayerService = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TelerportService = game:GetService("TeleportService")
local updatePartyEvent = ReplicatedStorage.UpdatePartyStatus
local universalIssueID = "Y1MzItBfyU"
local mainPlaceID = {["Urban Falls"] = 5041125493, ["East Bay Port"] = 5119882137}
local Party = {}
local PartyGroups = {}
Party.__index  = Party

--CONFIGURATIONS COMPONENT ORDER
--map
--public status
--rank requirement
--difficulty
--leader
--people

--Party Module
--Create the system where party groups are formed, and can be dealt with accordingly
function Party.new(configurations, play) -- Create a new party group
	local newPartyFormed = {} -- Metatable Work
	setmetatable(newPartyFormed, Party)
	table.insert(PartyGroups, newPartyFormed)
	
	newPartyFormed.Map = configurations.Map -- Attach the party settings to the party group
	newPartyFormed.PublicStatus = configurations.PublicStatus
	newPartyFormed.RankRequirement = configurations.RankRequirement
	newPartyFormed.Difficulty = configurations.Difficulty
	newPartyFormed.PartyLeader = play
	newPartyFormed.People = configurations.People
	
	return newPartyFormed, PartyGroups -- Send the new party group to the server
end

function Party.RetrieveUniversalId()
	return universalIssueID
end

function Party:Remove(yieldForPlayerExit, forceShutdown) -- Get rid of a party group. Optional argument to wait for all players to leave and to shutdown regardless.
	if table.find(PartyGroups, self) then
		if self.People then -- Checks if people values are there.
			if not forceShutdown then
				local canRemove = true -- Sees if players are out/party is ready to be removed
				for index, player in pairs(self.People) do
					if player then
						if yieldForPlayerExit then
							local found = false
							PlayerService.PlayerRemoving:Connect(function(plr)
								if plr == player then
									found = true
								end
							end)
						while not found do
							PlayerService.PlayerRemoving:Wait()
							game:GetService("RunService").Heartbeat:Wait()
						end
						if index >= #self.People then
							canRemove= true
						end
					else
						canRemove = false
					end
				end
			end
			
		
			if canRemove then  -- Sends back whether party can be removed
				ReplicatedStorage.PartyChange:FireAllClients(false, tostring(self), self)
				if workspace:FindFirstChild("PARTY: "..tostring(self)) then
					wait(1)
						workspace:FindFirstChild("PARTY: "..tostring(self)):Destroy()
				end
				table.remove(PartyGroups, table.find(PartyGroups, self))
				self = {}
				return true, PartyGroups
			else
				return false, PartyGroups
			end
		else
			for _, v in pairs(self.People) do
				if v and v ~= self.PartyLeader then
				ReplicatedStorage.PartySignal:FireClient(v, nil, "Shutdown")
				end
			end
			ReplicatedStorage.PartyChange:FireAllClients(false, tostring(self), self)
			if workspace:FindFirstChild("PARTY: "..tostring(self)) then
					workspace:FindFirstChild("PARTY: "..tostring(self)):Destroy()
				end
			table.remove(PartyGroups, table.find(PartyGroups, self))
			self = {}
			return true, PartyGroups
		end
	else
		ReplicatedStorage.PartyChange:FireAllClients(false, tostring(self), self)
		table.remove(PartyGroups, table.find(PartyGroups, self))
		self = {}
		return true, PartyGroups
	end
end

function Party:JoinRequest(requestingPlayer) -- Handles player requests
	if self.PublicStatus then -- If public server, automatically handle the joining process
		if not table.find(self.People, requestingPlayer) then 
			print()
			if #self.People < 6 then
				table.insert(self.People, requestingPlayer)
				ReplicatedStorage.UpdatePartyStatus:FireAllClients(self, tostring(self))
				ReplicatedStorage.EventSignal:FireAllClients(requestingPlayer)
				return true, PartyGroups
			else
				return false, PartyGroups, "FullParty"
			end
		else
			return false, PartyGroups, "NoIssue"
		end
	else -- If private, handle it with owner approval first
		local grantedStatus = ReplicatedStorage.PlayerRequest:InvokeClient(self.PartyLeader, requestingPlayer)
		print("GSIUF", grantedStatus)
		if grantedStatus and grantedStatus == requestingPlayer and #self.People < 6 then
			local inAnotherParty = false
			for pos, party in pairs(PartyGroups) do
				for _, player in pairs(party) do
					if player then
						if player == requestingPlayer then
							inAnotherParty = true
						end
					else
						table.remove(party, pos)
					end
				end
			end
			if not inAnotherParty then
				table.insert(self.People, #self.People +1, requestingPlayer)
				updatePartyEvent:FireAllClients(self, tostring(self))
				ReplicatedStorage.EventSignal:FireAllClients(requestingPlayer)
				return true, PartyGroups
			end
		else
			return false, PartyGroups, "NoIssue"
		end
	end
end

function Party:RemovePlayer(player)
	if player == self.PartyLeader then
		self:Remove(false, true)
	else
	for position, person in pairs(self.People) do
		if person == player then
			table.remove(self.People, position)
				updatePartyEvent:FireAllClients(self)
		end
	end
	end
	return PartyGroups
end

function Party:Teleport()
	if self.People then
		local people = {}
		for i, v in pairs(self.People) do
			if v then
				table.insert(people, v)
			else
				table.remove(self.People, i)
			end
		end
		pcall(function()
		local reservedServerKey = TelerportService:ReserveServer(mainPlaceID[self.Map])
		TelerportService:TeleportToPrivateServer(mainPlaceID[self.Map], reservedServerKey, people)
		end)
	end
end
return Party

From looking at your code you seem to be missing an end at line 62, however I could be wrong.