Disclaimer: I am only a beginner and Teleporting players seems to be my weakness.
Hi,
I am having trouble with my teleporter. If you walk through the teleporter and continue walking once you arrive, it turns into an endless loop. I wrote in the script that you need to teleport players 4 studs away to avoid this.
My Script
local TeleportPad1 = script.Parent.TeleportPad1
local TeleportPad2 = script.Parent.TeleportPad2
TeleportPad1.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild(âHumanoidRootPartâ) then
hit.Parent.HumanoidRootPart.CFrame = TeleportPad2.CFrame + TeleportPad2.CFrame.LookVector * 4 â Telepports the player a few studs away from the part, so it doesnât result in spam teleporting.
end
end)
TeleportPad2.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild(âHumanoidRootPartâ) then
hit.Parent.HumanoidRootPart.CFrame = TeleportPad1.CFrame + TeleportPad1.CFrame.LookVector * 4 â Telepports the player a few studs away from the part, so it doesnât result in spam teleporting.
I think what you did is actually the solution to the initial problem? What seems to be the issue now? Players get teleported with an offset so there should be no spam teleporting.
Just debounce it.
Set a variable boolean to true and then back to false at the end after a cooldown.
If the boolean is âtrueâ dont run the function.
local TeleportPad1 = script.Parent.TeleportPad1
local TeleportPad2 = script.Parent.TeleportPad2
local debounce = false
TeleportPad1.Touched:Connect(function(hit)
if debounce then return end
debounce = true
if hit.Parent:FindFirstChild(âHumanoidRootPartâ) then
hit.Parent.HumanoidRootPart.CFrame = TeleportPad2.CFrame + TeleportPad2.CFrame.LookVector * 4 â Telepports the player a few studs away from the part, so it doesnât result in spam teleporting.
end
wait(3) -- cooldown time
debounce = false
end)
TeleportPad2.Touched:Connect(function(hit)
if debounce then return end
debounce = true
if hit.Parent:FindFirstChild(âHumanoidRootPartâ) then
hit.Parent.HumanoidRootPart.CFrame = TeleportPad1.CFrame + TeleportPad1.CFrame.LookVector * 4 â Telepports the player a few studs away from the part, so it doesnât result in spam teleporting.
end
wait(3) -- cooldown time
debounce = false
end)
@UfoVR1337 Okay, I tried this and put the teleporters further apart. It still does it but you just need to walk a bit further. @Self_Reflexive I put local debounce = false at the beginning but nothing happened.
Teleport service does not have anything to do with your script.
Also I suggest you do not set the CFrame. Use :MoveTo(POS)
Anyways here would be the correct script to do this.
local TeleportPad1, TeleportPad2 = script.Parent:WaitForChild("TeleportPad1"), script.Parent:WaitForChild("TeleportPad2")
local Debounce1, Debounce2 = false, false
local HitDebounce = {}
TeleportPad1.Touched:Connect(function(Hit)
if not Debounce1 and not HitDebounce[Hit.Parent] and Hit.Parent:IsA("Model") and Hit.Parent:FindFirstChildOfClass("Humanoid") then
HitDebounce[Hit.Parent] = Hit.Parent
Debounce1 = true
Hit.Parent:MoveTo(TeleportPad2.Position + Vector3.new(0, 3, 0))
wait(.1)
Debounce1 = false
wait(2)
HitDebounce[Hit.Parent] = nil
end
end)
TeleportPad2.Touched:Connect(function(Hit)
if not Debounce2 and not HitDebounce[Hit.Parent] and Hit.Parent:IsA("Model") and Hit.Parent:FindFirstChildOfClass("Humanoid") then
HitDebounce[Hit.Parent] = Hit.Parent
Debounce2 = true
Hit.Parent:MoveTo(TeleportPad1.Position + Vector3.new(0, 3, 0))
wait(.1)
Debounce2 = false
wait(2)
HitDebounce[Hit.Parent] = nil
end
end)