function getClosestPlr()
local range = math.huge -- Set the range the dummy can look for players within --
local targets = {} -- A table for potential targets to look at --
local target = nil -- We will update this to the player's position later --
for i,v in pairs(workspace:GetChildren()) do
if v:FindFirstChild("Humanoid") and not v.Name == "Dummy" then -- Just make a if/then statement to check if v is not the dummy looking at the player--
table.insert(targets, v:FindFirstChild("Head")) -- Add the head's position into the table--
end
if #targets > 0 then
for i,k in pairs(targets) do
if (script.Parent.Position - k.Position).Magnitude < range then
target = k -- Sets the target to look at the player's head--
range = (script.Parent.Position - k.Position).Magnitude -- Inserts the distance between the dummy and the player's head--
end
end
end
end
return target
end
while wait() do -- This loop continuously searches for the closest player and looks at the player's head.
local target = getClosestPlr()
if target then
script.Parent.CFrame = CFrame.new(script.Parent.Position, target.Position)
end
end
This might not be the best method, but it works smoothly for me.