Part wont look at player?

I am making it so that a part in workspace is always looking at the player, but it won’t work

I get no errors, here is my script.

local part = script.Parent

local player = game.Players.LocalPlayer
local char = player.Character
local head = char:FindFirstChild("Head")


while true do
	part.CFrame = CFrame.lookAt(part.Position, head.Position)
	task.wait()
end
1 Like

You should Probably wait for the Character, or the Players Head.

1 Like

Is it a local script or a server script?
Is the Part Anchored?

localscript and the parts anchored

Localscripts don’t run in the workspace (I’m assuming this because you parented the script to the part)

1 Like

Keep the following in a LocalScript in StarterPlayer > StarterCharacter

2 Likes

They do, but they should be put into workspace after game spawns for the player

1 Like

I wouldn’t use a wait() function. Instead, I would add a WaitForChild() function to all the variables that include a path to a instance.
And instead of a while loop, I would use Roblox’s heartbeat.

local part = --Path
local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local head = character:WaitForChild("Head")

game:GetService("RunService").Heartbeat:Connect(function()
	part.CFrame = CFrame.lookAt(part.Position, head.Position)
end
1 Like

But if you use too many wait for child, the local script will have soo many yields sometimes throws an error called attempt to index nil

And yes heart beat is good, I was actually typing it, so I didn’t mention sorry my bad, sorry

1 Like

You’ve got a point. It is a bit better than one wait() function since different clients take different times to load, and sometimes might take longer than 1 second.

Small not: I also only used one WaitForChild() function in my script, which should be fine.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.