I want to make these NPCs rotate towards my avatar. They need to be welded to the bus stop, so I need to tween the weld C0. So far my code is this: (where “thing” is my avatar’s rootpart)
It looks like when its tweening the cframe the cframe’s position is being set to the origin or someplace far off. Try removing the inverse and see what the difference is.
Also why are they welded? Do they ever become unwelded and move around or get on a bus?
Because you could probably have easier cframe tweening if they are just anchored. You would not need to do the C0, but just the HumanoidRootPart’s CFrame.
That shouldn’t work because of how weld.C0 works, and I’ve just tried it and it’s worsened the issue. The C0 that it’s setting to is correct as if I set it without tweening it, it works. But when I tween it, that effect happens for some reason.
They need to be welded as eventually I’ll need to weld them to the bus so they can move while inside it
If I just set the CFrame to the lookAt code, it works as normal. The issue is using tween to interpolate between them. I’ve tried lerp and it’s done the same thing.
Ok, I did a test place where I try to get the same results.
It has 2 ways of doing it, one is by anchored npc’s the other is by welded npc’s I still think the anchored are better. RotateNpcsToPlayer.rbxl (100.1 KB)
The npc’s are in a folder, with a controlling script for each.
The Welded npc’s have a weld in HumanoidRootPart, and the C0 is by default just a CFrame.new()
local dir = script.Parent
local base = dir:WaitForChild("Base")
local speed = .2
local tweenInfo = TweenInfo.new(
speed,
Enum.EasingStyle.Linear
)
while true do
--Find closest player
local target = nil
for _,i in pairs(game.Players:GetPlayers()) do
if i.Character then
if target == nil then
target = i.Character
else
if (i.PrimaryPart.Position-base.Position).Magnitude < (target.PrimaryPart.Position-base.Position).Magnitude then
target = i.Character
end
end
end
end
if target then
for _,i in pairs(dir:GetChildren()) do
if i.Name == "Npc" then
local targetCFrame = CFrame.lookAt(i.PrimaryPart.Position,target.PrimaryPart.Position)
local tween = game.TweenService:Create(i.PrimaryPart,tweenInfo,{CFrame = targetCFrame})
tween:Play()
end
end
end
wait()
end
Here is the code for the WELDED npc’s
local dir = script.Parent
local base = dir:WaitForChild("Base")
local speed = .2
local tweenInfo = TweenInfo.new(
speed,
Enum.EasingStyle.Linear
)
while true do
--Find closest player
local target = nil
for _,i in pairs(game.Players:GetPlayers()) do
if i.Character then
if target == nil then
target = i.Character
else
if (i.PrimaryPart.Position-base.Position).Magnitude < (target.PrimaryPart.Position-base.Position).Magnitude then
target = i.Character
end
end
end
end
if target then
for _,i in pairs(dir:GetChildren()) do
if i.Name == "Npc" then
local weld = i.PrimaryPart.Weld
local rotationCFrame = CFrame.lookAt(i.PrimaryPart.Position,target.PrimaryPart.Position)
rotationCFrame = rotationCFrame - rotationCFrame.Position
local objectCFrame = rotationCFrame:Inverse()
local tween = game.TweenService:Create(weld,tweenInfo,{C0 = objectCFrame})
tween:Play()
end
end
end
wait()
end
Not totally sure how you are doing your code, based on just the little ‘facesomething’ function you shared, but maybe this well help you in some way.
I very much appreciate this, I wasn’t expecting somebody to try for themselves. I guess it improves your own coding skills. I’ll have a look now, thanks.
Never mind, I’ve got it. Part1.CFrame:Inverse()*Part0.CFrame works perfectly. Thanks a lot for that place file, it’s definitely put me in the right direction.