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?
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.
you put a extra ) there, just saying
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.
- We can teleport the part to a preassigned position, which will have to be re-defined in every script.
- 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!
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.