Index exists but table[index] returns nil?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    When a player clicks the button, this code gets executed to damage the player:
RS.Remotes.GameLogic.ClientDamage.OnServerEvent:Connect(function(player,dmgPlr)
	local damagedSession = Sessions.SESSIONS[dmgPlr]
	print(dmgPlr,Sessions.SESSIONS)
	print(damagedSession)
	
	damagedSession:SetHealth(damagedSession.lives - 1)
	Sessions:BroadcastLives()
end)

however when the line executes, damagedSession returns nil and next lines error.
image
I literally cannot comprehend why this errors because it works prior to players leaving. I had 3 players in the server (Player 1 to ID 1 and etc). Player 1 and 3 leaves, leaving only Player 2 with ID 2. Their data remains in Sessions.SESSIONS in index 2. Yet when I try to get it the table returns nil??

Are you sure dmgPlr is the correct type? It might be a string when it’s supposed to be number or vice-versa. That’s the only thing that I can see as a potential problem here.

2 Likes

:sob: :sob: :sob:

print(dmgPlr,typeof(dmgPlr),Sessions.SESSIONS)
	print(damagedSession)


Well that saved me time redesigning my system, thanks

also for more clarification, this table was passed through a remote event which apparently converted the indices into string for some reason

https://devforum.roblox.com/t/nonstring-indices-of-a-keyed-table-are-converted-to-strings-when-sent-through-a-remote/678499

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.