been coding for like 2 hours now and ive learned about variables and parents and children sort of, want to know how to make a teleporter just a part that teleports you to another part, if anyone can write a code and break it down for me I’d be very appriciative
local part1 = script.Parent
local part2 = game.Workspace.part2
local Debounce = false
part1.Touched:Connect(function(Hit) -- checks if part1 has been touched
if Hit.Parent:FindFirstChild("Humanoid") and Hit.Parent.Humanoid.Health >= 1 and debounce == false then -- if hit.Parent find a child named humanoid and the humanoids health is greater than 1 and debounce = false it executes
Hit.Parent.HumanoidRootPart.Position = part2.Position -- changes character position to part2
debounce = true -- changes debounce to true so you cant teleport again
task.wait(2) -- cooldown
debounce = false -- changes to false so now you can teleport
end
end)
i understand parts like the first 2 lines but could you break down how it works
So, the debounce is set to false of course and more will be said later on, basically it checks if part1 has been touched a then executes. It checks whatever it touched has a humanoid and the health of the humanoid is greater than 1 and debounce = false then it changes the humanoidrootpart position to the position of part2. after it changes debounce to true so that you can’t get teleported after 2 seconds then it sets debounce to false so then you can use it again. Basically, the debounce is for the if statement so if the debounce = false it executes if it = true then it doesn’t execute. And for the teleporting it just changes the position.
If you don’t understand I’m sorry that I was not in depth.
To make this simple, you really need to just change the position of the HumanoidRootPart. (which is inside the character)
Add it to a touched event. Something as basic as this
local teleporter = script.Parent
local char
teleporter.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild('Humanoid') then
char = hit.Parent
char.HumanoidRootPart.Position = Vector3.new(--Position would go here)
end
end
No need for debounces really since it’s a teleporter and it will move the character, but for good practice I 'd recommend it.
Sometimes with debounces it would execute multiple times so that’s why I have added it.
You could just make a teleporter like this.
function onTouched(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player then
game:GetService("TeleportService"):Teleport(8002949801,player)
end
end
script.Parent.Touched:connect(onTouched)
local player = game.Players:GetPlayerFromCharacter(p.Parent)
if player then
player.Character:SetPrimaryPartCFrame() -- Put Part Position in the parenthesis
end
end)
Sorry but if you have read the post, he clearly said part teleporters. Useful information though!
For my example this will be a normal script inside a model in workspace. Of course lets start with two part, lets name them PartA and PartB then group those two parts together into a model along with the script.
The code:
local PartA = script.Parent.PartA --- what **local** does is basically very much like what a variable does in math terms and can be called/name anything you like
local PartB = script.Parent. PartB
PartA.Touched:Connect(function(PartWhoHitPartA) --- This sets how the script is triggered/activated. **PartWhoHitPartA** is assigned to what touched PartA, it can be named anything you like
PartWhoHitPartA.CFrame = PartB.CFrame --- CFrame is very much like a parts Position, but CFrame brings the parts whole model or anything that is attached to it unlike Position
end) --- This ends the code/script
I hope this breaks it down for you, very simple and easy.
I actually have a working teleport system in my obby that allows for tens of teleport pairs. In terms of scripting, its closest to the solution @alexinkenski made
the way I did it is I make teleport pairs called 1, 1_receiverTeleport, 2, 2_receiverTeleport etc. and put them in a folder called Teleports.
Then I use a for-loop on that folder and connect all the just number named teleports (the non receivers) when touched to teleport the player into a receiver of matching name, using SetPrimaryPartCFrame
I can share the code if you want? its a bit complex if youre super new but i can explain it in detail