I’m not too sure what you want, but I think you want this -
Part behind.wmv (1.2 MB)
So this is the script
-- put this serverScript into game.StarterPlayer.StarterCharacterScripts
local tweenService = game:GetService("TweenService")
local part = game.Workspace.Part
local character = script.Parent
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
local moveBehindTween
local prevHumanoidRootPartPos = humanoidRootPart.Position
local timeToReachDest
local easingStyle = Enum.EasingStyle.Sine
local easingDirection = Enum.EasingDirection.InOut
local noOfTimesToRepeat = 0
local reverse = false
local delayForStart = 0
while wait(0.1) do
local humanoidRootPartPos = humanoidRootPart.Position
local humanoidRootPartOrientation = humanoidRootPart.CFrame - humanoidRootPart.Position
if humanoidRootPart == prevHumanoidRootPartPos then
timeToReachDest = 0.5
if moveBehindTween then
moveBehindTween:Cancel()
end
local tweenInfo = TweenInfo.new(timeToReachDest, easingStyle, easingDirection, noOfTimesToRepeat, reverse, delayForStart)
local targets = {
CFrame = humanoidRootPart.CFrame + humanoidRootPartOrientation:VectorToWorldSpace(Vector3.new(-5, 0, 0))
}
moveBehindTween = tweenService:Create(part, tweenInfo, targets)
moveBehindTween:Play()
else
timeToReachDest = 0.25
prevHumanoidRootPartPos = humanoidRootPart
if moveBehindTween then
moveBehindTween:Cancel()
end
local tweenInfo = TweenInfo.new(timeToReachDest, easingStyle, easingDirection, noOfTimesToRepeat, reverse, delayForStart)
local targets = {
CFrame = humanoidRootPart.CFrame + humanoidRootPartOrientation:VectorToWorldSpace(Vector3.new(0, 0, 5))
}
moveBehindTween = tweenService:Create(part, tweenInfo, targets)
moveBehindTween:Play()
end
end
Now it is a bit long script, but I will explain it
First of all, it uses tweening for a smooth transition. If you don’t know what tweening is, I suggest checking it out
So what this does it just every 0.1 seconds, it checks the humanoidRootPart’s position
it also has a variable called previousHumanoidRootPartPosition.
So the script just checks if the humanoids position has changed or not
If it has changed, then it sets the target CFrame to
humanoidRootPart’s Position + Vector3.new(0, 0, -5) (5 studs behind the humanoidRootPart’s Position)
So tweening comes into action and changes the position smoothly in 2.5 seconds
But if the position hasn’t changed then it just sets the target frame to
humanoidRootPart’s Position + Vector3.new(-5, 0, 0) (5 studs beside the humanoidRootPart’s Position)
And the tweening smoothly transitions it from its position to 5 studs beside the humanoidRootPart
And that’s basically it and I hope I helped.