I got this script from one of the old devforum posts but when testing it to my own purposes it doesn’t work as I try to, I want to update the UpperTorso Y and Z or X position at all times making my character able to look at a certain part CFrame/Position passed on by a remove event but the torso just stays in 1 position all the time or shifts away from the target any tips?
--state = what state is in
--Target for this demonstration is the head
Remotes.ReadyToRotate.OnServerEvent:Connect(function(plr, state, Target)
wait()
local Character = plr.Character
local WaistMotor = Character.UpperTorso.Waist
while state == true do
local NewMouseCFrame = Target.CFrame
local NewTestCFrame=CFrame.new(WaistMotor.Transform.p,NewMouseCFrame.p)
local Look = NewTestCFrame.LookVector
local percentage = (Look.X/(WaistMotor.C0.X - Look.X))
percentage = math.clamp(percentage,.1,1)
WaistMotor.C0 = WaistMotor.C0:Lerp(CFrame.Angles(0,Look.X,0),percentage)
Heartbeat:Wait()
end
end)
Here’s a modernized version of that. Just change the “targetPosition” to an upvalue that gets set from your RemoteEvent
-- StarterCharacter script
local player = game.Players.LocalPlayer
local cam = workspace.CurrentCamera
local char = player.Character
local human = char:WaitForChild("Humanoid")
local rootPart = char.PrimaryPart
local upperTorso = char:WaitForChild("UpperTorso")
local waistJoint = upperTorso:WaitForChild("Waist")
-- Services
local uis = game:GetService("UserInputService")
local runService = game:GetService("RunService")
local tweenService = game:GetService("TweenService")
-- Globals
local currentSpeed = 0;
local currentTween = nil;
human.Running:Connect(function(speed)
currentSpeed = speed
end)
local originalWaistC1 = waistJoint.C1
local maxRotation = math.rad(65)
runService:BindToRenderStep("vanityCamera", 400, function()
local targetPosition = (cam.CFrame * CFrame.new(0,0,-1000)).p
local torsoFront = rootPart.CFrame.LookVector
local torsoRight = rootPart.CFrame.RightVector
local vectorToTarget = (targetPosition - rootPart.Position)
local rotation = math.atan2(torsoRight:Dot(vectorToTarget), torsoFront:Dot(vectorToTarget))
-- Clamp the rotation
if (rotation < -maxRotation) then
rotation = -maxRotation
elseif (rotation > maxRotation) then
rotation = maxRotation
end
if (math.abs(rotation) == maxRotation and currentSpeed <= 0.5) then
-- Rotate the bottom half to face the new direction
local newRootPartCFrame = CFrame.new(rootPart.Position, Vector3.new(
targetPosition.X, rootPart.Position.Y, targetPosition.Z
))
currentTween = tweenService:Create(rootPart, TweenInfo.new(0.33), {
CFrame = newRootPartCFrame
})
currentTween:Play()
else
if (currentTween and currentSpeed > 0.5) then
if (currentTween.PlaybackState == Enum.PlaybackState.Playing) then
currentTween:Cancel()
end
end
waistJoint.C1 = CFrame.Angles(0,rotation,0) * originalWaistC1
end
end)
This is great and all but I’m not doing it so it faces my cursor, this is a sort of timed code which lasts 5 seconds where the the torso looks at the position
That’s not what my code does and I already explained how to change what it does. Are you asking me to write your code for you? The code to rotate the waist to face a targetPosition is in my code above.