Issue to make a tween service repeat [script]

Hello, I’m looking to make a tween service script to optimize the latency of my game. The tween starts as soon as the player touches a part and stops as soon as he touches another part (and at this moment the object goes back to its initial position to allow the player to do the parkour again). Except that I have the impression that the fact of teleporting the part cancels the tween service permanently and thus this one does not function any more once the player made the parkour once. Is there a way to make the tween work as many times as the player wants to do the parkour? Thank you

local plr = game.Players.LocalPlayer
local ts = game:GetService("TweenService")
local p1 = game.Workspace.avion1b
local p2 = game.Workspace.avion2b
local ball = game.Workspace.heliceb
local stop = game.Worskace.Stop
local TweenInformation = TweenInfo.new(
	17,
	Enum.EasingStyle.Linear,
	Enum.EasingDirection.Out,
	-1,
	true,
	2 
)
local Tween = ts:Create(ball, TweenInformation, {Position = p1.Position})
local Tween2 = ts:Create(ball, TweenInformation, {Position = p2.Position})
function onTouch(hit)
	if hit.Parent.Name == plr.Name then
		ball.CanTouch = false
		wait(2)
		Tween:Play()
		Tween2:Play()
	end	
end

ball.Touched:Connect(onTouch)

function onTouch(hit)
	if hit.Parent.Name == plr.Name then
		Tween:Cancel()
		Tween2:Cancel()
		ball.CFrame = CFrame.new(107.053, 643.35, -53.407)
	end
end
stop.Touched:Connect(onTouch)

I rephrase: Changing Cframe’s position of a part seems to definitely break the tween service. Anyone know how to fix this problem?

Why did you define onTouch twice?

I agree. You should probably do a different name for 2 different functions, or the computer will get confused and just give up

1 Like

I see that you set the CanTouch for ball to false so that it wouldn’t run the first onTouch function multiple times in a row, but I don’t see you ever set it back to true. I think this would cause the tweens to not play again after the first time because the touch events don’t get fired.

I would set CanTouch back to true right after you teleport the ball back to its original position.

function onTouch(hit)
	if hit.Parent.Name == plr.Name then
		Tween:Cancel()
		Tween2:Cancel()
		ball.CFrame = CFrame.new(107.053, 643.35, -53.407)
		ball.CanTouch = true
	end
end
stop.Touched:Connect(onTouch)
1 Like

Yes I just wrote the second part very (too) quickly, so I forgot some info, but @HMgamingTheSecondary is right I just had to change the name of the function

1 Like