Table.find returns nil instead of number even though having value

I have printed in many different ways. In the table when I do print(table[0]) and print(table[1]) they both return nil. Here is the code:

local chatrooms = {
	UnnamedVoiceChat = 'OwnerName'
}

renameroom.OnServerEvent:Connect(function(player, newname)
	if player.RoomOwnership.Value == nil then
		return error('Player Owns Nil')
	else
		print(player.RoomOwnership.Value) -- UnnamedVoiceChat  -  Server - ChatRooms:24
		local index = table.find(chatrooms,player.RoomOwnership.Value)
		if index == nil then
			return error('Could Not Find Index') -- Calls Error in Console
		else
			local room = chatrooms[player.RoomOwnership.Value]
			chatrooms[index] = nil
			chatrooms[newname] = chatrooms[index]
			chatrooms[newname] = player.Name
			renameroom:FireAllClients(player,room,newname)
		end
	end
end)
2 Likes

You can’t use table.find on a dictionary

1 Like

I have a function right above it that uses table.find and it works just find with the same dictionary

local chatrooms = {
	UnnamedVoiceChat = 'OwnerName'
}

repossess.OnServerEvent:Connect(function(player, room)
-	if table.find(chatrooms, room) ~= nil then
-		return error
-	else
		player.RoomOwnership.Value = room
		chatrooms[room] = player.Name
		repossess:FireAllClients(room, player)
	end
end)

renameroom.OnServerEvent:Connect(function(player, newname)
	if player.RoomOwnership.Value == nil then
		return error('Player Owns Nil')
	else
		print(player.RoomOwnership.Value)
		local index = table.find(chatrooms,player.RoomOwnership.Value)
		if index == nil then
			return error('Could Not Find Index')
		else
			local room = chatrooms[player.RoomOwnership.Value]
			chatrooms[index] = nil
			chatrooms[newname] = chatrooms[index]
			chatrooms[newname] = player.Name
			renameroom:FireAllClients(player,room,newname)
		end
	end
end)

table.find always returns nil when you’re using it with a dictionary.

It looks like you’re checking if a key exists in chatrooms. You can do that by just indexing chatrooms and checking if that’s nil. Here’s an example (I rewrote your repossess connection):

repossess.OnServerEvent:Connect(function(player, room)
	if chatrooms[room] ~= nil then
		error()
	else
		player.RoomOwnership.Value = room
		chatrooms[room] = player.Name
		repossess:FireAllClients(room, player)
	end
end)

1 Like

This helped alot, but how do i find the index?

Edit: I’ve searched this up and apparently i cant. I just have to figure out how to replace a value in a dictionary.

To replace a value in a dictionary:

local dict = {
	value = 5
}

print(dict.value) -- 5
dict.value = 10
print(dict.value) -- 10
1 Like

I meant the key sorry. Do you still know how?

Oh, I see now.

local dict = {
	key = 5
}

dict.newKey = dict.key
dict.key = nil

print(dict) -- { ["newKey"] = 5 }
1 Like

will dict.key = nil get rid of the entire key and value?

Yes. You can try printing it out.

still returns nil

renameroom.OnServerEvent:Connect(function(player, newname)
	if player.RoomOwnership.Value == nil then
		return error('Player Owns Nil')
	else
		print(player.RoomOwnership.Value)
		local key = chatrooms[player.RoomOwnership.Value]
		if key == nil then
			return error('Could Not Find Key')
		else
			chatrooms.newname = key
			renameroom:FireAllClients(player,key,newname)
			key = nil
		end
	end
end)

image

Print the chatrooms dictionary and the room ownership value and I’ll try to figure out what went wrong.

Also, chatrooms.newname = key should be chatrooms[newname] = key because newname is a variable.

1 Like

this: local key = chatrooms[player.RoomOwnership.Value] is still nil i dont know whats going on. It already exists under the value and table I checked.

Take a screenshot or copy and paste the output of the print statements here (do print(chatrooms) and print(player.RoomOwnership.Value). If the key-value pair exists in the dictionary, it shouldn’t return nil.

1 Like

player.RoomOwnership.Value is “For Sauce”, which doesn’t exist in the dictionary, so it’s returning nil. The problem, then, lies in some other area of your script where you set the room ownership value, so I’d suggest looking there. Maybe you forgot to update the dictionary when someone creates a room?

1 Like

It wasnt printing that earlier the script lies here

repossess.OnServerEvent:Connect(function(player, room)
	if chatrooms[room] ~= nil then
		error()
	else
		player.RoomOwnership.Value = room
		print(room..' For Sauce')
		chatrooms[room] = player.Name
		repossess:FireAllClients(room, player)
	end
end)

I removed the ~= and the script works oopsie doodles

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