HELLO! how do i make my NPCs body all face the player
my code:
Events.NoteDismissed.OnServerEvent:Connect(function(player)
-- Send remote event to client to fade out the UI elements
Events.NoteDismissed:FireClient(player)
print("Note has been dismissed, starting dialogue.")
end)
end)
After the note is dismissed, I want my NPCs to have a little tween where they turn around to face the player.
My directory: Workspace > NPCs
I’m just confused how to make the whole NPC spin you know?
You can tween each NPC’s CFrame to smoothly transition into one where they are facing the player. By the way, make sure your NPC’s also have a primary part (you can set this to the humanoid root part)
local TweenService = game:GetService("TweenService")
local NPCFolder = workspace:WaitForChild("NPCs")
local function tweenNPCToFacePlayer(npc, player)
-- checks
if not npc or not npc.PrimaryPart then return end
local character = player.Character
if not character or not character:FindFirstChild("HumanoidRootPart") then return end
local npcRoot = npc.PrimaryPart
local targetPos = character.HumanoidRootPart.Position
-- new rotated CFrame for the NPC, but only on Y-axis
local currentPos = npcRoot.Position
local lookAtPos = Vector3.new(targetPos.X, currentPos.Y, targetPos.Z)
local goalCFrame = CFrame.new(currentPos, lookAtPos)
local tweenInfo = TweenInfo.new(
1, -- adjust this tween duration
Enum.EasingStyle.Quad, -- I thought it would be fancier than linear
Enum.EasingDirection.Out
)
local tween = TweenService:Create(npcRoot, tweenInfo, {CFrame = goalCFrame})
tween:Play()
end
Then you call that function on each NPC:
Events.NoteDismissed.OnServerEvent:Connect(function(player)
-- Send remote event to client to fade out the UI elements
Events.NoteDismissed:FireClient(player)
print("Note has been dismissed, starting dialogue.")
local NPCfolder = game.Workspace.NPCs
for _, npc in ipairs(NPCfolder:GetChildren()) do
tweenNPCToFacePlayer(npc, player)
end
end)