✓ Accepted answer
Your current code only checks the character's position at the beginning of the game. I provided some code for you that works. You need to put it in the while loop for it to check every couple milliseconds. Still, you are not checking for the closest player, you are only having the script look at the local player.
getClosestPlayer() gets the closest player by checking their distance from the part.
with that information, we can use CFrame.new() to change the part's position.
CFrame.new() has to parameters, the first parameter being the position you want the part to be at, and the second parameter being a position you want the part to look at.
CFrame.new(position, lookAt)
since we got the closest player with getClosestPlayer(), we can make the part's 'lookAt' parameter be the player's humanoidRootPart. which means the part looks at the closest player.
part = script.Parent
function getClosestPlayer()
local closestPlayer = nil
local closestMagnitude = math.huge
for _, player in pairs(game:GetService("Players"):GetPlayers())do
if(player.Character)then
local magnitude = (player.Character.HumanoidRootPart.Position - part.Position).magnitude
if(magnitude < closestMagnitude)then
closestPlayer = player
end
end
end
return closestPlayer
end
while(wait())do
local closestPlayer = getClosestPlayer()
if(closestPlayer)then
part.CFrame = CFrame.new(part.Position, closestPlayer.Character.HumanoidRootPart.Position)
end
end
1 comment
Thank you, that helped me so much!