Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Teleport a player depending on the stringvalue?

It only says "No station was selected" on the output even the value is different, How do i fix this?

local StationToTeleport = script.Parent.Parent.StationToTeleport.Value
local HumanoidRootPart = game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart")
local TeleportLocations = game.Workspace.TeleportLocations

script.Parent.MouseButton1Click:Connect(function()
	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)

2 answers

✓ 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

0

I agree, this is an easier way to do this.

0

I think you meant to change the value of String1 in your example, then the output changes aswell

0

Thank you very much!

Instead of using local StationToTeleport = script. Parent.Parent.StationToTeleport.Value elseif StationToTeleport == "Anonas" the

Try using this instead

local StationToTeleport = script.Parent.Parent. StationToTeleport elseif StationToTeleport.Value == "Anonas" the

I've removed the "Value" in your Variable.

This discussion is preserved as it was. New answers, comments, and votes are closed.