Tweening with CFrame.new(pos, LookAt)

I have a script where a part attempts to turn towards the player once via. a CFrame.new(pos, LookAt) tween. However, it doesn’t work and gives me an index nil error for the player’s HumanoidRootPart.

local Players = game:GetService("Players")
local plr = Players:FindFirstChildWhichIsA("Player") or Players.PlayerAdded:Wait()
local Character = plr.Character

local TS = game:GetService("TweenService")
local Object = script.Parent
local TW = TweenInfo.new(0.5, Enum.EasingStyle.Linear, Enum.EasingDirection.Out)
local LOOK = TS:Create(Object, TW, {CFrame = CFrame.new(script.Parent.Position, Vector3.new(plr.Character.HumanoidRootPart.Position.X, Object.Position.Y, plr.Character.HumanoidRootPart.Position.Z))})

wait(7)

LOOK:Play()

I’m assuming this is a Script. Instead of defining plr in your way, create a PlayerAdded event with CharacterAdded instead - it’s likely that the player turned out nil. You’d also want assure Object is directed correctly.

I’d advise establishing a Character:WaitForChild("HumanoidRootPart") if the issue persists.

Sorry for the extremely late reply, but after specifying a PlayerAdded event, do I make a variable inside based on the player that joined?

No - the player is already defined in the parameter for game.Players.PlayerAdded:Connect(function(plr).

Oh, alright then.

thirtycharacters

What’s the status? Is it working? Has the issue been resolved? In case if you’re struggling:

--Script in ServerScriptService
local ts = game:GetService("TweenService")
local obj = --child to tween

game.Players.PlayerAdded:Connect(function(plr)
    wait(7)
    ts:Create(obj, TweenInfo.new(.5, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0), {CFrame = CFrame.new(script.Parent.Position, Vector3.new(plr.Character.HumanoidRootPart.Position.X, Object.Position.Y, plr.Character.HumanoidRootPart.Position.Z))}):Play()
end)
3 Likes

Now, your solution on the top wasn’t really working (probably because my code wasn’t correct when using that method), but this certainly worked! I should really start using PlayerAdded more, heh.

Thanks again for the help! I really appreciate it!

By the way, how could I make the plr a variable that could be used anywhere in the script? I was thinking of making tweens that would occur at any point in the script, and not just within a PlayerAdded function.

Of course you can make it anywhere - the

ts:Create(obj, TweenInfo.new(.5, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0), {CFrame = CFrame.new(script.Parent.Position, Vector3.new(plr.Character.HumanoidRootPart.Position.X, Object.Position.Y, plr.Character.HumanoidRootPart.Position.Z))}):Play()

can be used anywhere, at any time.

1 Like

Ah, I see then.

thirtycharacters