Hello, im trying to make a secret area where if you touch a part you teleport to there.
the problem is its not teleporting the player there. how can i fix this?
my code:
local Teleport = "teleport2"
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)
If something is Locked it just means you can’t select it in Studio, so it’s not needed for in-game scripts.
The “MoveTo” function doesn’t teleport players but walks the player to that location, if you want to teleport the Player ,change the HumanoidRootPart CFrame to the CFrame of the Teleport2 part
here’s a script that should work
local Teleport = "teleport2"
function Touch(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local Pos = script.Parent.Parent:FindFirstChild(Teleport)
hit.Parent:FindFirstChild("HumanoidRootPart").CFrame = CFrame.new(Pos.Position)
end
end
script.Parent.Touched:Connect(Touch)
you add a debounce, let me make one real quick (srry for the wait)
local Teleport = "teleport2"
local debounce = false
function Touch (hit)
if hit.Parent:FindFirstChild("Humanoid") and not debounce then
debounce = true
local Pos = script.Parent.Parent:FindFirstChild(Teleport)
hit.Parent:FindFirstChild("HumanoidRootPart").CFrame = CFrame.new(Pos.Position)
wait(5) -- you can change this to however long you want the wait time to be
debounce = false
end
end
script.Parent.Touched:Connect(Touch)
script.Parent.Touched:Connect(function(hit)
local Plr = game.Players:GetPlayerFromCharacter(hit.Parent) -- Gets the player
if Plr then
Plr.Character:MoveTo(Pos.Position + Vector3.new(0,2,0) -- so the player doesnt spawn inside the part
end
end)
srry about that , my autocorrect change “CFrame” to “Cframe”
this should work
local Teleport = "teleport2"
local debounce = false
function Touch (hit)
if hit.Parent:FindFirstChild("Humanoid") and not debounce then
debounce = true
local Pos = script.Parent.Parent:FindFirstChild(Teleport)
hit.Parent:FindFirstChild("HumanoidRootPart").CFrame = CFrame.new(Pos.Position)
wait(5) -- you can change this to however long you want the wait time to be
debounce = false
end
end
script.Parent.Touched:Connect(Touch)
Debounce = false
WaitTime = 2 -- Change this if you like
script.Parent.Touched:Connect(function(hit)
if not Debounce then
Debounce = true
local Plr = game.Players:GetPlayerFromCharacter(hit.Parent) -- Gets the player
if Plr then
Plr.Character:MoveTo(Pos.Position + Vector3.new(0,2,0)) -- so the player doesnt spawn inside the part
end
task.wait(WaitTime)
Debounce = false
end
end)
teleport1 = teleport you to teleport block 2
teleport2 = teleport you to teleport block 1
NOT SCRIPT
and this is the code in teleport1
local Teleport = "teleport2"
local debounce = false
function Touch (hit)
if hit.Parent:FindFirstChild("Humanoid") and not debounce then
debounce = true
local Pos = script.Parent.Parent:FindFirstChild(Teleport)
hit.Parent:FindFirstChild("HumanoidRootPart").CFrame = CFrame.new(Pos.Position)
wait(5) -- you can change this to however long you want the wait time to be
debounce = false
end
end
script.Parent.Touched:Connect(Touch)
and this is the code for teleport2
local Teleport = "teleport1"
local debounce = false
function Touch (hit)
if hit.Parent:FindFirstChild("Humanoid") and not debounce then
debounce = true
local Pos = script.Parent.Parent:FindFirstChild(Teleport)
hit.Parent:FindFirstChild("HumanoidRootPart").CFrame = CFrame.new(Pos.Position)
wait(5) -- you can change this to however long you want the wait time to be
debounce = false
end
end
script.Parent.Touched:Connect(Touch)
The wait is for when you can use the teleporter again , so it doesn’t keep teleporting you every second
pls may you explain in what way the wait isn’t working?
Debounce = false
WaitTime = 2 -- Change this if you like
script.Parent.Touched:Connect(function(hit)
if not Debounce then
Debounce = true
local Plr = game.Players:GetPlayerFromCharacter(hit.Parent)
if Plr then
Plr.Character:MoveTo(Pos.Position + Vector3.new(0,2,0))
end
task.wait(WaitTime)
Debounce = false
end
end)
I don’t know the solution but there’s a lot of working teleporters in the free models. Maybe you can compare the script of theses free models to your teleporter.