How do I reference a object?

I’m in the middle of making a marble race inside of Roblox Studio and I want to make a platform that teleports the marble to another position, I know how to reference a player, but how do I do the same with a object like a marble?

Where is the marble parented in the data model?

Because if you know the parent of the marble, then you just do [Insert parent].Marble

If you do not know the parent of the marble, you could do something like workspace:FindFirstChild("Marble,"true), although I don’t recommend this because it can be performance heavy.

1 Like

I have all of my marbles in the same folder, would the folder be the parent?

Yes.

charrrrrrrrrrrrrrrrrrrrrr

1 Like

Yes, the folder is the parent. So you would first get a reference to the folder like doing this:

local folder1 = workspace.FOLDER_NAME
local folder2 = folder1.FOLDER_NAME2
--Etc, if you have more folders to go through.

--Alternatively, if you want to do it in one line of code, you can do something like this.
local all_folder = workspace.FOLDER_NAME.FOLDER_NAME2.REST_OF_FOLDERS

Once you know the parent, then it is as simple as doing something like this:

local marble1 = YOUR_FOLDER_REFERENCE.YOUR_MARBLE_NAME

If your folder paths have any names with spaces then instead of using the “.” to get references, you would use something like this:

local folder = workspace["THIS IS A FOLDER"]
local folder2 = folder["THIS IS ANOTHER FOLDER"]
1 Like

Is this how I could code it?

Close, but here is a better way of doing it.

local Folder = workspace.Marbles -- (there is a variable that defines workspace, so you don't need to reference "game")
local Teleporter = script.Parent

local function Teleport()
     Folder.MarbleName.Position = Vector3.new(1,2,PUT_Z_COORDINATE)
end

Teleporter.Touched:Connect(Teleport)

Make sure to replace MarbleName with your actual Marble’s name and make sure to put the Z coordinate in the Vector3 (Vector2s are used for 2D things like Gui, not game objects)

1 Like

I have multiple marbles inside of the folder, what should I do then?

Pretty simple fix, you just make a for loop to teleport them all.

local Folder = workspace.Marbles -- (there is a variable that defines workspace, so you don't need to reference "game")
local Teleporter = script.Parent

local function Teleport()
     for _i,marble in pairs(Folder:GetChildren()) do
          marble.Position = Vector3.new(1,2,PUT_Z_COORDINATE)
     end
end

Teleporter.Touched:Connect(Teleport)

With this one, you don’t have to actual get the marble’s name. It expects all children of the Marbles folder to be a marble.

1 Like

I don’t want it to teleport all the marbles, I just want it to teleport the one that touches it.

Ah, I think I know what you mean. You just want marbles that touch the teleporter to teleport, yes?

Yet another simple fix.

local Folder = workspace.Marbles -- (there is a variable that defines workspace, so you don't need to reference "game")
local Teleporter = script.Parent

local function Teleport(part)
     --Checks to see if the part that touches the teleporter is a child of a folder called "Marbles"
     if part.Parent.Name == "Marbles" and part.Parent:IsA("Folder") then
           --This must be a marble then, so let's teleport it.
           part.Position = Vector3.new(1,2,PUT_Z_COORDINATE)
     end
end

Teleporter.Touched:Connect(Teleport)
1 Like

Do you mean that you want to teleport it with TeleportService?

If not, just do:

marble.Touched:Connect(function(partThatTouchedMarble)
if partThatTouchedMarble.Name == "Platform" then
marble.Position = positionToTeleportTo -- Vector3.new(number1,number2,number3)
end
end
1 Like