"Attempt to call a nil value"

I know this question has been asked, I’ve tried looking into this issue, and I know what the error refers to - I’m apparently trying to call a value that doesn’t exist. The problem is, this errors solution is typically unique to the script that contains the error, and I don’t know exactly what is considered nil in this case. I feel slightly stupid for even having to ask this, but this problem always stumps me.

This script is supposed to insert the player into a table when they join, and remove them from the table when they leave. Simple.

local playersList = {}

function playerAdded(plr)
	playersList.Insert('plr') -- error occurs on this line
    print(''..plr.Name..' | '..plr.Team.Name..'')
end

function playerRemoved(plr)
    playersList.Remove('plr') -- and this line
	print(''..plr.Name..' left the game')
end

players.PlayerAdded:Connect(playerAdded)
players.PlayerRemoving:Connect(playerRemoved)

The players variable is defined at the start of the script - it’s not the cause of the error. The player IS a valid value, as removing the two lines that cause the error, everything works fine. The players name and team is printed when they join, and the disconnect message is printed when they leave. I tried making the function wait a few seconds so the player could actually load in, but that just delayed the error.

I am not used to working with tables, I understand their basic functions and usages, but errors like this usually fly over my head heh-

1 Like

Tables dont have the table library built into them like strings, you need to call the library yourself

local playersList = {}

function playerAdded(plr)
	table.insert(playersList, plr)
    print(''..plr.Name..' | '..plr.Team.Name..'')
end

function playerRemoved(plr)
    table.remove(playerList, table.find(playerList, plr))
	print(''..plr.Name..' left the game')
end

players.PlayerAdded:Connect(playerAdded)
players.PlayerRemoving:Connect(playerRemoved)
2 Likes

Ahh, I see. That fixed it right up and now the script is working as intended, thank you! I’ll be sure to use tables properly next time.

1 Like