How would I compare IntValue with a value inside of a table?

Hello fellow Robloxians,

I, being a dummy when it comes to tables, have encountered an issue trying to check whether the IntValue is equal to the value inside of a table. What I have saved in both values is the number of the car spawner since there’s going to be multiple. To teleport the vehicle to the right CFrame I need to get the teleportCFrame, however I can’t get the CFrame because when checking whether the values are the same, it attempts to index nil with plrSpawner. Here are the important parts of my code, please point out any dumb mistakes I’ve made.

local currentUsers = {}

local function createUserTable(spawner)
	local userTable =  {
		["plrSpawner"] = spawner.Value
	}
	
	return userTable
end

local function addUser(userTable, plr)
	if userTable then
		table.insert(currentUsers, plr.Name)
		currentUsers[plr] = userTable
	else
		return
	end
end
local spawnerNumber = spawner:WaitForChild("SpawnerNumber")
if spawnerNumber.Value == currentUsers[plr.Name]["plrSpawner"] then
     teleportCFrame = spawner:WaitForChild("TeleportCFrame").CFrame
end

If you need anything else, please let me know.

Not 100% able to grasp what the script is trying to do, but I do see what the error is.
if spawnerNumber.Value == currentUsers[plr.Name]["plrSpawner"]

The thing is, you never set the table with an index of “plr.Name.” Let me try to explain. When you did table.insert(current users, plr.Name), what you actually did was made the table look like this:

currentUsers = {
[1] = "d4rkn_ess"
}

But when you say currentUsers[plr.Name], the script is looking for the table to look like this:

currentUsers =
{
["d4rkn_ess"] = --Some value
}

Also, I think you made a mistake up here: currentUsers[plr] = userTable

I’m assuming you aren’t trying to create a new key to the table-- currentUsers[plr] is not the same as currentUsers[plr.Name].

So, like I said, I don’t really know what your script is trying to do so I might not be right about this next part, but I think what you should do is this instead:

	if userTable then
		currentUsers[plr.Name] = userTable
    else
--rest of your script
1 Like

Thanks for pointing my mistake out, it finally works.

1 Like