Code doesn't recognise table and player name

Hello!

In my local script I have it when a player leaves the server it sends a remote event which the local script recieves and if it can find the table and player.name then it will delete the text label.

But it cannot find the table and/or player.name and sends out the warning.

local script:

local PlayersFrame = script.Parent

local playerTable = {}

game.ReplicatedStorage.CloseFunction.LeaveServer.OnClientEvent:Connect(function(playerName)
	print("Player Left Server")
	if table.find(playerTable, playerName) and PlayersFrame:FindFirstChild(playerName) then
		local playerNameIndex = table.find(playerTable, playerName)
		if playerNameIndex then
			print(playerNameIndex)
		end
		local label = PlayersFrame:FindFirstChild(playerName)
		label:Destroy()
		table.remove(playerTable, playerNameIndex)
	else
		warn("Could not find table")
	end


end)

It does print out playerNameIndex but sends out the warning. This is not the full local script since the rest is unnecessary

1 Like

is that the full script? If so, you are never adding the players to the playerTable, causing it to always be empty.

1 Like

Here is the full script:

-- Create a wait variable to debounce the cloning of the text label
local debounce = false

local Players = game:GetService("Players")
local PlayerNameText = script.Parent.PlayerNameText

local myServerName
local InServer = false
local PlayerName = ""

local PlayersFrame = script.Parent

local playerTable = {}

game:GetService("ReplicatedStorage").JoinServers.JoinServerPlayer.OnClientEvent:Connect(function(...)
	
	local players = {...}
	for _, playerName in ipairs(players) do
		if not table.find(playerTable, playerName) and not PlayersFrame:FindFirstChild(playerName) then
			local newLabel = PlayerNameText:Clone()
			newLabel.Name = playerName
			newLabel.Text = playerName
			newLabel.Parent = PlayersFrame
			newLabel.Visible = true
			table.insert(playerTable, playerName)
		end
	end


	--[[if not debounce then
		debounce = true
		wait(1)
		debounce = false

		InServer = true

		for i, player in pairs(game.Players:GetPlayers()) do

			if not script.Parent:FindFirstChild(player.Name) then

				local playerTextClone = PlayerNameText:Clone()
				playerTextClone.Text = player.Name
				playerTextClone.Parent = script.Parent
				playerTextClone.Name = player.Name
			end
		end
	end]]
end)

-- 

game:GetService("ReplicatedStorage").ServerNames.OnClientEvent:Connect(function(player, ServerName)
	-- Invokes in ServerIDHandler
	print(ServerName .. "PLAYERS SCRIPT")

	myServerName = ServerName


end)

game.ReplicatedStorage.CreateServers.CreateServerPlayer.OnClientEvent:Connect(function(plrname)

	local label = PlayerNameText
	label.Text = tostring(plrname)
	label.Parent = script.Parent
	label.Name = tostring(plrname)
	InServer = true
end)

game.ReplicatedStorage.PLRNames.OnClientEvent:Connect(function(plrname) 
	task.wait(0.1)
	if InServer == true then
		print(tostring(plrname)) 
		local playerTextClone = PlayerNameText:Clone()
		playerTextClone.Text = tostring(plrname)
		playerTextClone.Parent = script.Parent
		playerTextClone.Name = tostring(plrname)
		PlayerName = plrname
	
		-- InServer = false
	else
		print("Player is not in server")
	end


end)

game.ReplicatedStorage.CloseFunction.LeaveServer.OnClientEvent:Connect(function(playerName)
	print("Player Left Server")
	if table.find(playerTable, playerName) and PlayersFrame:FindFirstChild(playerName) then
		local playerNameIndex = table.find(playerTable, playerName)
		if playerNameIndex then
			print(playerNameIndex)
		end
		local label = PlayersFrame:FindFirstChild(playerName)
		label:Destroy()
		table.remove(playerTable, playerNameIndex)
	else
		warn("Could not find table")
	end


end)

The big yellow comment part was my old bit to that remote event.

1 Like

So it looks like you’re doing this for some sort of leaderboard system, right?
Is there a particular reason you need to use your own RemoteEvent instead of just using Players.PlayerAdded and Players.PlayerRemoving events of the Players service?

1 Like

Im not making a leaderboard system. And players join a ui server that a player creates which then fires a remote event. Sorry for the confusion.

1 Like

I don’t know if this is directly related to the problem, but try to use dictionary instead of an array for managing playerTable.

local playerTable = {}

-- add player
playerTable[playerName] = true

-- remove player
playerTable[playerName] = nil

if playerTable[playerName] and PlayersFrame:FindFirstChild(playerName) then
-- Player exist
else
-- Player does not exist
end