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.
01 | local pad 1 = workspace:WaitForChild( "TelePart1" ) --Change "TelePart1" to the name of the part the player touches to teleport to the other part |
02 | local pad 2 = workspace:WaitForChild( "TelePart2" ) --Change "TelePart2" to the name of the part the player gets teleported to |
03 |
04 | local cooldownTime = 5 --This is the number of seconds before the player can teleport again |
05 | local teleportHeight = 50 --Change this to how many studs above the teleport pad you want the player to teleport to |
06 |
07 | --The code below does not need to be modified |
08 |
09 | local teleportPads = { pad 1 ,pad 2 } --Table with both teleport pads |
10 | local cooldown = false --Bool that stops player from quickly teleporting back and forth |
11 |
12 | for i, pad in ipairs (teleportPads) do |
13 | pad.Touched:Connect( function (part) --If player touches either teleport pad |
14 | if part.Parent:FindFirstChild( "Humanoid" ) and cooldown = = false then --If the touching part is a player and the cooldown is over |
15 | local humanoid = part.Parent:FindFirstChild( "Humanoid" ) |
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:
01 | script.Parent.Touched:Connect( function (part) |
02 | local humanoid = part.Parent:FindFirstChild( "Humanoid" ) |
03 | local button 1 = script.Parent |
04 | local button 2 = workspace [ "TelePart" ] -- change this to the name of the brick you're teleporting the player to |
05 | if humanoid then -- if a humanoid is found then |
06 | part.Parent.HumanoidRootPart.CFrame = CFrame.new(button 1. Position) -- teleport the player |
07 | button 2. Script.Disabled = true |
08 | script.Disabled = true |
09 | wait( 3 ) -- determines how long the teleport is disabled for |
10 | button 2. Script.Disabled = false |
11 | script.Disabled = false |
12 | end |
13 | 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
1 | button 2. Script.Disabled = true |
and
1 | button 2. Script.Disabled = false |