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