My teleport script spam teleports me, how do I fix it?

Heya there everybody! I made a teleport script and it spammed teleported me from one block to another. So I got help from a person in the Unofficial DevForum Community Discord, and added Debounce to the script. It now looks like this:

local debounce = false
function onTouch(part)
	if not debounce then
		h = part.Parent:FindFirstChild("Humanoid")
		if (h~=nil) then
			debounce = true
			h.Parent:MoveTo(script.Parent.Parent.Tele2.Position)
			task.wait(3)
			debounce = false
		end
	end
end
script.Parent.Touched:Connect(onTouch)

Yet it still does this:
https://cdn.discordapp.com/attachments/828251644503785494/884457563511468124/Teleport_script_not_working.mp4

I knew I progressed a little bit because it didn’t rapidly teleport me, it only teleported me twice…one time to and one time back. I looked over the script, and still couldn’t find the issue. I am a new developer and I am just starting to get the hang of scripting, if anyone could help me with this script to make it work, that would be AMAZING.

I think the issue is that you have 2 individual scripts that each have their own debounce variable, so the moment you teleport to the script.Parent.Parent.Tele2 Part, it’s basically thinking that you didn’t touch Tele2 just yet and so it teleports you back

You could just think of it like this:

  • Part 1: Touched & Teleported (Wait 3 Seconds for that individual Script to cooldown)
  • Part 2: Touched & Teleported (Wait 3 Seconds for that individual Script to cooldown)
  • Part 3: Touched & Teleported (Wait 3 Seconds for that individual Script to cooldown)

All of the Teleport Scripts basically have their own debounce set to false and etc, etc

What you could do, is reference both parts inside 1 script to let both of them know when to actually fire the debounce at the proper times so that it doesn’t teleport you twice:

local Part1 = script.Parent
local Part2 = Part.Parent.Tele2

local DB = false

local function Touch1(Hit)
    local H = Hit.Parent:FindFirstChildWhichIsA("Humanoid")

    if not DB and H then
        DB = true

        H.Parent:MoveTo(Part2.Position)
        task.wait(3)
        DB = false
    end
end

local function Touch2(Hit)
    local H = Hit.Parent:FindFirstChildWhichIsA("Humanoid")

    if not DB and H then
        DB = true

        H.Parent:MoveTo(Part.Position)
        task.wait(3)
        DB = false
    end
end

Part1.Touched:Connect(Touch1)
Part2.Touched:Connect(Touch2)
1 Like