Hello everyone! So on my game I need it so that when a part is touched you get telported to somewere else in the game. I can’t find that anywhere on the forums only ones that can teleport you to another game/place. I just have no idea were I should start with the teleport bit.
An instance of this was found:
In general, the teleportation utilizes CFrame
to move the entire character assembly to a specific position somewhere else.
Im not a very good scripter, but get this basic idea…
Talking about teleport, meaning that you wanted to move the character from the position is on and then to the location it has to go to. The thing you gotta configure of is the Player.Position I believe so, which obvious means the player’s position. It’ll be like this at the end:-
Let’s say that there are two different coloured pads, blue and red. Your goal is to teleport from blue to red
1)Player touched blue
In code:
2)Check to see whether the player touched the blue pad
3)If true, then set Player.Position (the location of the blue pad) to Player.Position(the location of the red pad)
4)Test run the code, got errors, fix and debug them.
5)Congrats! You’ve made your very own teleporter
*Sorry IDK about the real script of it btw
Try this:
Place this script inside of a part and make another part called Part2 then you place that where ever you want.
local Teleport = "Part2" --Your Part name.
function Touch (hit)
if script.Parent.Locked == false and script.Parent.Parent : FindFirstChild(Teleport).Locked == false then script.Parent.Locked = true script.Parent.Parent:FindFirstChild(Teleport).Locked=true
local Pos = script.Parent.Parent:FindFirstChild(Teleport)
hit.parent:moveTo(Pos.Position) wait(1)script.Parent.Locked = false script.Parent.Parent:FindFirstChild(Teleport).Locked = false
end
end
script.Parent.Touched:connect(Touch)
Try changing the position of the character like so:
local part = workspace.Part --Your part name, ofc
part.Touched:Connect(function()
local HRP = workspace:FindFirstChild("HumanoidRootPart") --Torso if your game is R6
HRP.Position = -- Your position, or you could just use another part's position!
end)
When you want to teleport a player when a part is touched, I’m assuming this is not a 2 way teleporter, so for this you can utilize SetPrimaryPartCFrame
and the Touched
event.
To start, we will make a variable for the place we are teleporting to. To keep it simple, make this another part in workspace
.
local destination = workspace.Part --Define the part you are teleporting to.
Next, we can set up a Touched
event to tell when a player has touched the part that this script is inside of. In this case, that is likely a pad of some sorts.
local destination = workspace.Part
script.Parent.Touched:Connect(function(part)
--This is the Touched event. It fires when a player touches the script's parent.
end)
Touched returns a parameter which is the part that touched the part the Touched event is connected to. We now need to check if that part belongs to a player’s character. We can do this by checking if the part’s parent has a Humanoid.
local destination = workspace.Part
script.Parent.Touched:Connect(function(part)
if part.Parent:FindFirstChildOfClass("Humanoid") then
--FindFirstChildOfClass will find the Humanoid regardless of it's name.
end
end)
Now we can teleport the player. For this, we can utilize SetPrimaryPartCFrame
which will set the CFrame of the player’s HumanoidRootPart
to the CFrame of the destination they are being teleported to.
local destination = workspace.Part
script.Parent.Touched:Connect(function(part)
if part.Parent:FindFirstChildOfClass("Humanoid") then
part.Parent:SetPrimaryPartCFrame(destination.CFrame + Vector3.new(0, 2, 0))
--Vector3.new(0, 2, 0) will add a 2 unit offset on the y-axis so the player doesn't get stuck inside the ground.
end
end)
I hope this helped! Reply if you have any questions or need further explaining.
Whenever someone Touches the Part you want to change the position of the HumanoidRootPart were you want the player to go.
To achieve this goal, simply change CFrame
of HumanoidRootPart inside player’s character to your desired value. Like so:
-- player:instance "Player", cframe:CFrame.new
local function tp(player,cframe)
if player and player:IsA("Player") then
local char = player.Character;
local hrp = char:FindFirstChild("HumanoidRootPart");
if hrp then
hrp.CFrame = cframe + Vector3.new(0,3,0); -- we're adding + 3 studs to not glitch the character
end;
else
error("Invalid 'player' argument: nil or not a Player");
end;
end;
-- add a event or something, and call the function
local plr; -- make sure 'plr' is not nil
local part = workspace.Part; -- change this variable to whatever part you'd wish the player to teleport to (or place, so just a CFrame.new value)
tp(plr,part.CFrame);
I’d recommend using this in a serverscript.
Good luck, have a nice day!
script.Parent.Touched:Connect(function(hit)
if hit and hit.Parent and hit.Parent:FindFirstChild("HumanoidRootPart") then
hit.Parent.HumanoidRootPart.CFrame = CFrame.new() --Put position you want player to teleport to here
end
end)