Character not tweening correctly?

So I’m trying to tween the character up 15 studs from where the raycast hits, however the problem i’m having is the tween completes in weird ways and i cannot understand why, sometimes It stops very early, only a couple studs up, in rare occasions it will complete the 15 stud tween, can anyone help me understand why?

	local hrp = char:WaitForChild("HumanoidRootPart")
	local humanoid = char:WaitForChild("Humanoid")
	hrp.Anchored = true
	

	local params = RaycastParams.new()
	params.FilterType = Enum.RaycastFilterType.Exclude
	params.FilterDescendantsInstances = {char}

	local rayOrigin = hrp.Position
	local rayDirection = Vector3.new(0, -50, 0)
	local raycastResult = workspace:Raycast(hrp.Position, rayDirection, params)

	if raycastResult then
		local currentPos = hrp.CFrame
		local targetCFrame = CFrame.new(currentPos.X, raycastResult.Position.Y + 15, currentPos.Z)
			
		local CFrameValue = Instance.new("CFrameValue")
		CFrameValue.Value = char:GetPivot()
		CFrameValue.Changed:Connect(function(newValue)
			char:PivotTo(newValue)
		end)
		
		local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Sine, Enum.EasingDirection.Out)
		local tween = TweenService:Create(CFrameValue, tweenInfo, {Value = targetCFrame})
		
		tween:Play()
	end
end

I can also attach a video if necessary

Looks like raycastResult is going to be true during the tween so the tween code will keep running during the current tween. The tween probably doesn’t have time to finish before it starts a new tween using the new values.

Either add a debounce or a task.wait(delayTime) just after the tween:Play() and see what happens.

1 Like

You’re right that does fix it but I still don’t understand why, shouldn’t it only play once since its not in any kind of loop or run service?

Two possible reasons for it.

  1. The raycast is always active and gets a new .hit value during the tween
  2. Your connected to CFrameValue.Changed, so if it changes (likely) you get a new PivotTo(newValue)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.