script.Parent.Touched:Connect (function(part) local humanoid = part.Parent:FindFirstChild("Humanoid") part.Parent.HumanoidRootPart.CFrame = CFrame.new(workspace["TelePart"].Position) wait(3) end)
code here whenever i step on my teleporter it keeps teleporting me non stop against my will any fixes?
Hello! I come with a solution! The code below is simple and easy to modify for your game and I will instruct you how to do so. Let me know if you need any help! All you need to do is create a script anywhere in the workspace and put the code in there. Then change lines 1 and 2 to be the parts you want the player to teleport to and from.
local pad1 = workspace:WaitForChild("TelePart1") --Change "TelePart1" to the name of the part the player touches to teleport to the other part local pad2 = workspace:WaitForChild("TelePart2") --Change "TelePart2" to the name of the part the player gets teleported to local cooldownTime = 5 --This is the number of seconds before the player can teleport again local teleportHeight = 50 --Change this to how many studs above the teleport pad you want the player to teleport to --The code below does not need to be modified local teleportPads = {pad1,pad2} --Table with both teleport pads local cooldown = false --Bool that stops player from quickly teleporting back and forth for i, pad in ipairs(teleportPads) do pad.Touched:Connect(function(part) --If player touches either teleport pad if part.Parent:FindFirstChild("Humanoid") and cooldown == false then --If the touching part is a player and the cooldown is over local humanoid = part.Parent:FindFirstChild("Humanoid") cooldown = true if pad == pad1 then part.Parent.HumanoidRootPart.CFrame = CFrame.new(pad2.Position.X,pad2.Position.Y+teleportHeight,pad2.Position.Z) else part.Parent.HumanoidRootPart.CFrame = CFrame.new(pad1.Position.X,pad1.Position.Y+teleportHeight,pad1.Position.Z) end wait(cooldownTime) --The wait time before the player can teleport cooldown = false end end) end
The problem with your script was the fact that there was no way to prevent the script from firing again once it touched another part; the wait() at the end of the script just makes the script wait before ending it, so multiple limbs could come into contact with the buttons and cause the script to fire multiple times, which results in an endless loop of teleportation.
To prevent this, add "script.disabled = true" to the script like this:
script.Parent.Touched:Connect(function(part) local humanoid = part.Parent:FindFirstChild("Humanoid") local button1 = script.Parent local button2 = workspace["TelePart"] -- change this to the name of the brick you're teleporting the player to if humanoid then -- if a humanoid is found then part.Parent.HumanoidRootPart.CFrame = CFrame.new(button1.Position) -- teleport the player button2.Script.Disabled = true script.Disabled = true wait(3) -- determines how long the teleport is disabled for button2.Script.Disabled = false script.Disabled = false end end)
This also disables any extra teleport scripts (such as one located in the part you're teleporting the players to) to prevent the script from teleporting the player back to the original part. If you don't have any extra scripts, remove these lines
button2.Script.Disabled = true
and
button2.Script.Disabled = false