Di bawah ini aku share sebuah script untuk object yang ingin kau buat selalu menghadap ke player yang terdekat dengan dia:
-- Script ini harus dimasukkan ke dalam objek yang ingin selalu menghadap ke pemain terdekat
-- Misalnya, masukkan ke dalam Script di dalam Part yang ingin diputar
local part = script.Parent -- Objek yang akan diputar (misalnya, Part)
local players = game:GetService("Players")
-- Fungsi untuk menemukan pemain terdekat
local function findNearestPlayer()
local nearestPlayer = nil
local shortestDistance = math.huge
for _, player in ipairs(players:GetPlayers()) do
if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
local humanoidRootPart = player.Character.HumanoidRootPart
local distance = (part.Position - humanoidRootPart.Position).Magnitude
if distance < shortestDistance then
shortestDistance = distance
nearestPlayer = player
end
end
end
return nearestPlayer
end
-- Fungsi untuk memutar objek menghadap pemain terdekat
local function lookAtNearestPlayer()
local nearestPlayer = findNearestPlayer()
if nearestPlayer and nearestPlayer.Character and nearestPlayer.Character:FindFirstChild("HumanoidRootPart") then
local humanoidRootPart = nearestPlayer.Character.HumanoidRootPart
local direction = (humanoidRootPart.Position - part.Position).Unit
-- Mengatur orientasi objek agar menghadap ke pemain
part.CFrame = CFrame.lookAt(part.Position, part.Position + direction)
end
end
-- Jalankan fungsi secara berkala
while true do
lookAtNearestPlayer()
wait(0.1) -- Tunggu 0.1 detik sebelum memeriksa lagi
end