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.
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.