Help getting a certain value from a table

If i have a table formatted in

Table = {["one"] = 1, ["two"] = 2}

If I were to use something like table.sort() how would i get both “one” and 1 aka the name for the table value and the value itself.

Try this:

Table["one"]
-- Alternate:
Table.one

I dont have time rn

Im trying to go more practical, Im making a teleport system based on input so if a player chooses to go to lets say a castle, the player type castle and based on that it goes through a table and chooses the [“Castle”] = CFrame/Position value

Ok so Quick Question: Are you teleporting to a Location, or a game

Edit: oof

It can go either way based on how i’m doing it
the place can be whatever its just a value and a value name

["Castle"] = PlaceId,
["Castle"] = CFrame

its interchangeable based on the formatting

Right now I have

Table = {["one"] = 1, ["two"] = 2}
if table.find(Destinations,DesiredDestination:lower()) then
		print("Found")
end

So i made this script:

local MapTelePos = workspace.Map -- What i used

p = game.Players.LocalPlayer


TableOfMap = {

	["Castle"] = {
		[1] = MapTelePos.Castle.CFrame	

	}



}



p.Chatted:Connect(function(m) -- Gets the Message Sent
	for i,tab in pairs(TableOfMap) do
		if m == i then -- Checks if the Message is equal to something in the table
			p.Character.PrimaryPart.CFrame = TableOfMap[m]1] -- Teleports
		end
	end
end)

Should work when a Player chats, unless i missed something

You can use this as a template if so

If you’d like to check the key of the dictionary you’d need to use a for loop.

Input:

local t = {["one"] = 25, ["two"] = 2,}

for Key,value in pairs(t) do
     print(Key,value)
     if Key == "one" then
          print(true)
     end
end

Output:

one	25
true
two	2
1 Like

You don’t need to loop thru the table, you can just get the cframe value from the table directly with Table[index]

local locations = {
    Castle = CFrame.new(69, 420, 1337)
}

-- some string of input that you got from wherever
local inputString = "Castle"

local teleportCFrame = locations[inputString]

if teleportCFrame then
    teleportTo(teleportCFrame)
else
    warn("No location with name " .. inputString)
end

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