How to use tween service to rotate the sentry's head and other welded parts?

local TweenService = game:GetService("TweenService")
local Players = game:GetService("Players")
local Debris = game:GetService("Debris")

local Sentry = script.Parent
local Sentry_Head = Sentry:WaitForChild("Sentry_Head")
local Ray_Origin = Sentry_Head:WaitForChild("Ray_Origin")

local MAX_RANGE = 100
local COOLDOWN = 0.1

local function Visualizer(Origin, End_Position, Distance, LASER_COLOR)
	local Laser = Instance.new("Part")
	Laser.CanCollide = false
	Laser.CanTouch = false
	Laser.CanQuery = false
	Laser.CastShadow = false
	Laser.Anchored = true
	Laser.Color = LASER_COLOR
	Laser.Material = Enum.Material.Neon
	Laser.Size = Vector3.new(0.1, 0.1, Distance)
	Laser.CFrame = CFrame.lookAt(Origin, End_Position) * CFrame.new(Vector3.new(0, 0, -Distance/2))
	Laser.Parent = workspace
	
	Debris:AddItem(Laser, 0.1)
end

while true do
	for i, player in Players:GetPlayers() do
		if not player.Character or not player.Character:FindFirstChild("HumanoidRootPart") then continue end
		
		local character = player.Character or player.CharacterAdded:Wait()
		local HRP = character:WaitForChild("HumanoidRootPart")
		
		local Distance = (Sentry_Head.Position - HRP.Position).Magnitude
		if Distance > MAX_RANGE then continue end
		
		local Origin = Ray_Origin.WorldCFrame.Position
		local Direction = HRP.Position - Origin
		
		local RaycastParameters = RaycastParams.new()
		RaycastParameters.FilterType = Enum.RaycastFilterType.Exclude
		RaycastParameters.FilterDescendantsInstances = {Sentry}
		RaycastParameters.IgnoreWater = true
		
		local RayResults = workspace:Raycast(Origin, Direction, RaycastParameters)
		
		if not RayResults then
			print("Hit Nothing!")
		else
			print("Hit: ".. RayResults.Instance.Name)
			
			local Hit_Instance = RayResults.Instance
			local Hit_Position = RayResults.Position
			local Hit_Distance = RayResults.Distance
			
			if Hit_Instance.Name == "Handle" and Hit_Instance.Parent:IsA("Accessory") and Players:GetPlayerFromCharacter(Hit_Instance.Parent.Parent) or Players:GetPlayerFromCharacter(Hit_Instance.Parent) then
				local LookAt_CFrame = CFrame.lookAt(Origin, Hit_Position)
				local X, Y, Z = LookAt_CFrame:ToOrientation()
				local New_Orientation = Vector3.new(math.deg(X), math.deg(Y), math.deg(Z))
				
				TweenService:Create(Sentry_Head, TweenInfo.new(0.25, Enum.EasingStyle.Linear), {Orientation = New_Orientation}):Play()
				Visualizer(Origin, Hit_Position, Hit_Distance, Color3.fromRGB(0, 255, 0))
			else
				Visualizer(Origin, Hit_Position, Hit_Distance, Color3.fromRGB(255, 0, 0))
			end
		end
	end
	
	task.wait(COOLDOWN)
end


I’m trying to use tween service to rotate the sentry’s head smoothly to look at the player but the welded parts won’t rotate along with it. I don’t know how to fix this.

Are the welded parts unanchored and are the weld constraints active & enabled?

I’m pretty sure they should be. I’ve done it before but let me check again.


TweenService is not something I would recommend for this, consider using an AlignOrientation constraint to smoothly er-orient an assembly of welded parts. This is what I personally to use rotate NPC characters in my game.

I mean I can try using it but can it tween the primary part and the welded instances?

In my experience TweenService pretty much ignores physics and this sort of thing just isn’t what it’s made for. Setting up an AlignOrientation constraint on the primary part is pretty simple and does effectively the same thing. You just need to set the CFrame of the second attachment to have the rest of of the model smoothly rotate to “catch up” with the change. You can adjust the “Responsiveness” of the constraint to change how fast this happens.

Will it also rotate the welded instances and how would I destroy it after the player moves out of range? :thinking:

Yes, as long as the primary part and welded parts are unanchored. You can disable the rotation behavior by setting the constraint’s Enabled to false.


I’m not sure if this was supposed to happen

It’s supposed to be facing the part from the FRONT right..?