How to make a part teleport upon click detector click?

Ok so, I need this for a table service script, I want when say, a table mat is clicked, antother part to teleport to its exact position, so it makes for a realistic table service, I am not familiar with scripts, so does anyone have any advice or help?

1 Like

You’d need a click detector in each table with a script inside it. On the click make a function happen that will teleport the tool or part to the table.

Well yeah I know that, but what goes inside the function and :Connect brackets?

How it works is, think of it like this:

game.Workspace.Table.ClickDetector – This portion of the script gets you the clickdetector inside the table.
game.Workspace.Table.ClickDetector.MouseClick – The added “MouseClick” is an event, it checks if the clickdetector has been clicked.
game.Workspace.Table.ClickDetector.MouseClick:Connect(function() – The added “:Connect(function())” Basically connects the “Event” to a function. So its like this:
game, > workspace, > table, > clickdetector, when mouseclick, connect to function.
a function needs to have an “end of file” it determines where the function ends.

game.Workspace.Table.ClickDetector.MouseClick:Connect(function()
end)

The ‘end’ in this case acts as the “end of file”. The bracket is there to close the open bracket before the word “function”.

Now. The (function() brackets are called parameters. In them, you put things called arguements (things that you can get from the function) for example. If you have a .Touched event and you connect it to a function. You can get the part that touched the other part.
part.Touched:Connect(function(ITEM_THAT_TOUCHED)

In addition, to achieve the affect you want you will wanna do what @xaIency said,
I hope this helped you.

4 Likes

you put a extra ) there, just saying

1 Like

Lets go through this step by step.

First, you’d need your basic click detector and script. You can either make an overarching script which handles all of the clicks for each table, or just copy and paste the scripts to each specific table. I will explain the second option as it is easier.

With the script in hand, you’ll now need to define your variables. In this, we will need to define the ClickDetector and the part to teleport. Let’s say it looks something like this.

local CD = script.Parent.ClickDetector -- script is just the script
local Part_To_Teleport = game.Workspace.PART_HERE -- the teleport part.

Now we’ve defined that, we’ll need to create the function to actually call whenever the Click Detector (which I’ll refer to as CD from now on) fires/runs. To set up a function, I’ll leave you to read @InternallyAmplified’s post, as that explains it pretty well.

Now you’ve added the function and are ready to code, let’s see what it looks like now.

local Part_To_Teleport = game.Workspace.PART_HERE -- the teleport part.

game.Workspace.Table.CD.MouseClick:Connect(function()

  -- Code time

end)

Now, we need to actually add the teleporting part. For this, there are 2 ways we can go about this.

  1. We can teleport the part to a preassigned position, which will have to be re-defined in every script.
  2. We can teleport the part relative to the table.

I’m going to explain both ways.

In way 1, you’ll need to choose a position, i.e Vector3.new(5,0,0) (that’s just the position of 5x, 0y and 0z). So, now you know the position and have it ready in mind, lets position the part at that point. To do this, all we have to do is Part_To_Teleport.Position = Vector3.new(5,0,0). Now, if you put that all together, you get:

local Part_To_Teleport = game.Workspace.PART_HERE -- the teleport part.

game.Workspace.Table.CD.MouseClick:Connect(function()

  Part_To_Teleport.Position = Vector3.new(5,0,0)

end)

Boom, route 1 finished.

Now, for route 2, you’ll need to do some Vector adding. Let’s say you want the Teleport part 5 studs up relative to the table. This time, we’ll need to get the table, which if its the parent then we’ll just say script.Parent, but if it’s inside of a model we’ll need to find it with something like script.Parent.Table

Now we have the table, we’ll need to add the Vector3 with the value of (0, 5, 0), which is simply just 5 studs up. Position in roblox coding is always done with a vector, however you can also position with CFrames.

Now, to add a Vector to another Vector, we’ll just need to do Vector1 + Vector2. In our case, Vector1 is script.Parent.Table.Position because we know that is a Vector. We also know that Vector2 is (0, 5, 0), as that it what you have chosen.

If we add this all together while making the Teleport_Parts Position equal to that, we will get it done.

The code should look like so:

local Part_To_Teleport = game.Workspace.PART_HERE -- the teleport part.

game.Workspace.Table.CD.MouseClick:Connect(function()

  Part_To_Teleport.Position = script.Parent.Table.Position + Vector3.new(0,5,0)

end)

Once you’ve done that, you’re done!

1 Like

https://gyazo.com/5aea7890446db48a57cd77af8ac56d7a Thank you!

Is this possible to move certain parts if certain tools are equipped, it is for waitering purposes, if you don’t know, it is fine, I can probably find a way to do it

I do, in fact, know how to do this, however it may be a bit more complicated.

Now that we need to see if the player has the tool out, we’ll need to, well, obtain the player. This is pretty simple, as the CD allows us to get the player. All we need to do is type in a variable name in the CD’s function brackets, making it look like this:

local CD = script.Parent.ClickDetector
local Part_To_Teleport = game.Workspace.PART_HERE -- the teleport part.

game.Workspace.Table.CD.MouseClick:Connect(function(player) -- The "player" variable is here.

  Part_To_Teleport.Position = Vector3.new(5,0,0)

end)

Now we have the player, we’ll need to see if they have the tool out. Recently, however, I figured out an easier way to see if the tool is equipped. To do this, we will need to know the name of the tool. Let’s pretend the tool is called “Ben” because why not.

Knowing the tools name, we can see if it’s in the player’s character. To get the player’s character, we’ll need to do local char = player.Character or player.CharacterAdded:Wait() as so there is no chance the script doesn’t break due to the character not being there.

With the character, we can see if we can find the tool by doing local tool = char.Ben because the tool is called Ben. However, we now need to see if the tool is actually there. If it isn’t, tool will be equal to nil, and if we assume it is there then there is a chance the code will break.

To make sure the tool is there, just do a simple if not tool then return end, which will end the function if the tool is equal to nil. Remember, it will only be nil if it’s not found.

If we add this all to the code, we get this.

local CD = script.Parent.ClickDetector
local Part_To_Teleport = game.Workspace.PART_HERE -- the teleport part.

game.Workspace.Table.CD.MouseClick:Connect(function(player) -- The "player" variable is here.

 local char = player.Character or player.CharacterAdded:Wait()
 local tool = char.Ben
 if not tool then return end

 Part_To_Teleport.Position = Vector3.new(5,0,0)

end)

And… voila! We’re done!

For some reason, with a tool equipped, it won’t allow me to click the part, which is frustrating, sorry to bother, but do you know why that is?

It seems that I made a few errors in the code, and have so adjusted them. I’ll go through the errors so you know what I did wrong and so you don’t make the errors yourself.

Error 1: Although I already defined CD, I called it from the Workspace despite having it as a variable already, and CD is not a valid child of Table.

Error 2: This is less of an error, but more of an improvement. I changed the line tool = char.NAME to tool = char:FindFirstChild("NAME")

Error 3: This is a fault of my own while testing, however the clickDetector didn’t seem to fire when there was an Activate event inside of the tool, or just any script at all. Still confused on it but eh.

Now, that’s all the errors I faced. Here is the fixed code:

local CD = script.Parent.ClickDetector
local Part_To_Teleport = script.Parent -- the teleport part.

CD.MouseClick:Connect(function(player) -- The "player" variable is here.

 local char = player.Character or player.CharacterAdded:Wait()
 local tool = char:FindFirstChild("jesus")
 if not tool then return end

 Part_To_Teleport.Position = Vector3.new(5,0,0)

end)

Enjoy. :smiley: