✓ Accepted answer
Almost every data type in Lua is copied when you create a variable of it, exception are tables, when you pass table to a function you can edit it's contents which will edit every variable referring to the table.
In your case you declare [StationToTeleport] as [SomeObject.Value] where [.Value] is a string, strings are not exactly copied but i won't confuse you so yes strings are also copied when you create a variable to them, example:
local String1 = "cat"
local String2 = String1
String2 = "another cat"
print(String1, String2)
cat, another cat
So when you change value of the "StringValue", the variable does not change because it has already copied the string and does not care about other references. To fix it, you need to create new string every time someone clicks the button which will get the current value:
local StationToTeleportValue = script.Parent.Parent.StationToTeleport
local HumanoidRootPart = game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart")
local TeleportLocations = game.Workspace.TeleportLocations
script.Parent.MouseButton1Click:Connect(function()
local StationToTeleport = StationToTeleportValue.Value
if StationToTeleport == "None" then
print("No station was selected")
elseif StationToTeleport == "Anonas" then
HumanoidRootPart.CFrame = TeleportLocations.Anonas.CFrame
elseif StationToTeleport == "Cubao" then
HumanoidRootPart.CFrame = TeleportLocations.Cubao.CFrame
elseif StationToTeleport == "Emerald" then
HumanoidRootPart.CFrame = TeleportLocations.Emerald.CFrame
elseif StationToTeleport == "Katipunan" then
HumanoidRootPart.CFrame = TeleportLocations.Katipunan.CFrame
elseif StationToTeleport == "Masinag" then
HumanoidRootPart.CFrame = TeleportLocations.Masinag.CFrame
elseif StationToTeleport == "Santolan" then
HumanoidRootPart.CFrame = TeleportLocations.Santolan.CFrame
elseif StationToTeleport == "Spawn" then
HumanoidRootPart.CFrame = TeleportLocations.Spawn.CFrame
end
end)
If you however want simpler and better code you can use tables which will store all the possible station names:
local StationToTeleportValue = script.Parent.Parent.StationToTeleport
local HumanoidRootPart = game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart")
local TeleportLocations = game.Workspace.TeleportLocations
local StationTeleportCases = {
"Anonas",
"Cubao",
"Emerald",
"Katipunan",
"Masinag",
"Santolan",
"Spawn",
}
script.Parent.MouseButton1Click:Connect(function()
local StationToTeleport = StationToTeleportValue.Value
if StationToTeleport == "None" then
print("No station was selected")
else
for _, Station in ipairs(StationTeleportCases) do
if Station == StationToTeleport then
HumanoidRootPart.CFrame = TeleportLocations[Station].CFrame
end
end
end
end)
using ipairs it loops through table of all existing station names and if the matching station is the [StationToTeleport] it will find it's CFrame in the [TeleportLocations] folder since names of stations and their locations match in your code.
3 comments
I agree, this is an easier way to do this.
I think you meant to change the value of String1 in your example, then the output changes aswell
Thank you very much!