I want to make a map teleporter where it will teleport the player when they click the ui for a map to the place but I want it to take the place name and ID from the table and teleport them if the name of the textbutton matchs the name of the ids table name.
local TextButton = nil --Define TextButton location here.
local LP = game:GetService("Players").LocalPlayer
local TPService = game:GetService("TeleportService")
local MapHandlerModule = require(ModuleLocation) --Define ModuleLocation here.
local MapTable = MapHandlerModule.Maps
if not typeof(MapTable) == "table" then error("Missing Maps!") end --Makes sure the table exists.
TextButton.MouseButton1Click:connect(function()
local DesiredId = MapTable[TextButton.Name] --You can make the FetchedId be based off of text instead by changing TextButton.Name to TextButton.Text
if typeof(DesiredId) == "number" then --Makes sure a value exists for the given name.
TPService:Teleport(DesiredId, LP)
end
end)
Alternatively, if you want to do it for every button:
local TextButtonsLocation = nil --Define TextButton's Parent location here.
local LP = game:GetService("Players").LocalPlayer
local TPService = game:GetService("TeleportService")
local MapHandlerModule = require(ModuleLocation) --Define ModuleLocation here.
local MapTable = MapHandlerModule.Maps
if not typeof(MapTable) == "table" then error("Missing Maps!") end --Makes sure the table exists.
for i,v in ipairs(TextButtonsLocation:GetChildren()) do
if v:IsA("TextButton") then
v.MouseButton1Click:connect(function()
local DesiredId = MapTable[v.Name] --You can make the FetchedId be based off of text instead by changing TextButton.Name to TextButton.Text
if typeof(DesiredId) == "number" then --Makes sure a value exists for the given name.
TPService:Teleport(DesiredId, LP)
end
end)
end
end