Di Roblox Studio, kamu bisa nambahkan object apa aja, lalu tambahkan script ini di dalamnya supaya dia bisa mengejar player terdekat:
local cube = script.Parent
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
-- BodyVelocity (gerakan)
local bodyVelocity = Instance.new("BodyVelocity")
bodyVelocity.MaxForce = Vector3.new(1e6, 1e6, 1e6)
bodyVelocity.Parent = cube
-- BodyGyro (rotasi terkunci & stabil)
local bodyGyro = Instance.new("BodyGyro")
bodyGyro.MaxTorque = Vector3.new(1e6, 1e6, 1e6) -- kunci semua axis
bodyGyro.P = 10000
bodyGyro.Parent = cube
-- Fungsi cari player terdekat
local function getClosestPlayer()
local closestPlayer = nil
local shortestDistance = math.huge
for _, player in pairs(Players:GetPlayers()) do
if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
local distance = (player.Character.HumanoidRootPart.Position - cube.Position).Magnitude
if distance < shortestDistance then
shortestDistance = distance
closestPlayer = player
end
end
end
return closestPlayer
end
-- Loop kejar player
RunService.Heartbeat:Connect(function()
local targetPlayer = getClosestPlayer()
if targetPlayer and targetPlayer.Character then
local targetRoot = targetPlayer.Character:FindFirstChild("HumanoidRootPart")
if targetRoot then
local direction = (targetRoot.Position - cube.Position).Unit
local speed = 14 -- ubah sesuai kebutuhan
bodyVelocity.Velocity = direction * speed
-- Atur rotasi: hanya menghadap horizontal player (sumbu Y terkunci)
local lookAt = Vector3.new(targetRoot.Position.X, cube.Position.Y, targetRoot.Position.Z)
bodyGyro.CFrame = CFrame.new(cube.Position, lookAt)
end
end
end)
-- Bunuh player kalau tersentuh
cube.Touched:Connect(function(hit)
local character = hit.Parent
if character and character:FindFirstChild("Humanoid") then
character.Humanoid.Health = 0
end
end)