Can't find Player in table

I’m trying to create a party system for a horror game. Recently, i’ve been trying to implement the removal of players from parties when they leave from the game. However, I can’t seem to do it because of a bug I can’t find.

Here are some excerpts from the code that i’m using:

Party Module functions:

function module:removePlayer(player: Player, party : table)
	local FindPlayer = table.find(party.Players, player)
	if FindPlayer then
		local IsOwner = (party.Leader == player)
		table.remove(party.Players, FindPlayer)
		party.CurrentPlayers -= 1
		if #party.Players > 0 then
			if IsOwner == true then
				party.Leader = party.Players[math.random(1,#party.Players)]
			end
		else
			module:Delete(party)
		end
		table.remove(party.Players, FindPlayer)
		PlayerRemovedEvent:Fire(party)
		return true, "Successfully removed "..player.Name.." from the party."
	else
		return false, "Couldn't remove "..player.Name.." from the party."
	end
end


function module:GetPartyPlayerIsIn(player: Player): "Party Table"
	for _, Party in pairs(Parties) do
		print(Party)
		for _, Member in pairs(Party.Players) do
			print("Found ", Member)
			if Member == player then
				return Party
			end
		end
	end
	return nil
end

Server script:

Players.PlayerRemoving:Connect(function(player)
	print(player.Name," left")
	local PartyPlayerIsIn = Party:GetPartyPlayerIsIn(player)
	print("PartyPlayerIsIn is ", PartyPlayerIsIn)
	if PartyPlayerIsIn == nil then return end
	Party:removePlayer(player, PartyPlayerIsIn)
	print("removed player successfully!!")
end)

Party structure:

	local PartyInfo = {
		["ID"] = #Parties+1;
		["Leader"] = leader;
		["MaxPlayers"] = clientData.MaxPlayers;
		["AccessType"] = clientData.AccessType;
		["CurrentPlayers"] = 0;
		["Players"] = {};
	}

GetPartyPlayerIsIn doesn’t return the Player Instance despite the player being in the table. I have made prints in the console to check if the player was there, and it was. How could I fix this issue?

Maybe a type mismatch? Check the following condition:

local isPlayer = typeof(Member) == "Instance" and Member:IsA("Player")
print(isPlayer)

If it returns false the Member variable may be a string giving the same output as printing the player object or a custom OOP object you have defined, meaning that the comparison will always assume the statement is false(unless you define custom behavior by using the __eq attribute of metatables).

1 Like

I’ve just implemented and tested your code, and it seems that this is not the problem, as it is identifying the object as a Player.
image