Teleporting teleports multiple times after touched

hey! I am making a game when you need to teleport after you touch a button. However when I touch it, it teleports me, and when I try to move, it just teleports me back to the position about 2 more times.
script:

local tweenService = game:GetService("TweenService")

local elavator = script.Parent

local tweenInfo = TweenInfo.new(3,Enum.EasingStyle.Linear,Enum.EasingDirection.Out,0,false,0.1)
local properties = {Position = Vector3.new(-57.5, 13.3, 17.5)}
local tween = tweenService:Create(elavator,tweenInfo,properties)


local db = false
script.Parent.Touched:Connect(function(hit)
	if not db then
		db = false
		
		wait(5)
		tween:Play()
		wait(5)
		local humanoid = hit.Parent:FindFirstChild("Humanoid")
		if humanoid ~= nil then
		humanoid.Parent.HumanoidRootPart.CFrame = CFrame.new(workspace["teleport1"].Position)
		wait(100)
		local db = true
	end
	end
end)

Try changing the line “local db = true” to “db = true”, your basically setting db to true only in that section in the code and not in the entire thing. You are also setting db to false when its already false, which I’m guessing you meant to make it true there.

Heres what I would do to try to fix it:

local db = false
script.Parent.Touched:Connect(function(hit)
	if db == false then
		db = true
		wait(5)
		tween:Play()
		wait(5)
		local humanoid = hit.Parent:FindFirstChild("Humanoid")
		if humanoid ~= nil then
		humanoid.Parent.HumanoidRootPart.CFrame = CFrame.new(workspace["teleport1"].Position)
		wait(100)
		db = false
	end
	end
end)
2 Likes