← KEMBALI KE SCRIPT LIBRARY

Ayam (Chicken Farm)

UmumVersi 1.0Update 24/11/2026

Ayam otomatis dibuat (model, animasi jalan/makan/kepak, suara) -- dan berkumpul di sekitar SEMUA Part bernama "ChickenPlace". -- Dibuat di sisi pemain (client) supaya ringan.

Cara penggunaan
Full Tutorial di YouTube iBalDBK
Lokasi pemasangan
Taruh sebagai LocalScript di StarterPlayer > StarterPlayerScripts
Script
-- ChickenFarm
-- Taruh sebagai LocalScript di StarterPlayer > StarterPlayerScripts.
-- Ayam otomatis dibuat (model, animasi jalan/makan/kepak, suara)
-- dan berkumpul di sekitar SEMUA Part bernama "ChickenPlace".
-- Dibuat di sisi pemain (client) supaya ringan.

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

-- ===================== PENGATURAN =====================
local CHICKEN_COUNT = 12        -- jumlah ayam total (dibagi rata ke tiap titik)
local CHICKEN_SCALE = 1         -- UKURAN ayam (0.5 = kecil, 1 = normal, 2 = besar)
local CENTER_PART_NAME = "ChickenPlace" -- titik kumpul = semua Part dengan nama ini
local CENTER_POSITION = Vector3.new(0, 0, 0) -- dipakai kalau Part tidak ada
local ROAM_RADIUS = 30          -- jarak ayam berkeliaran dari tiap titik kumpul
local SPEED_MIN = 3             -- kecepatan jalan paling lambat
local SPEED_MAX = 6             -- kecepatan jalan paling cepat
local TURN_SPEED = 4            -- kecepatan berbelok
local STUDS_ALL_SIDES = true    -- true = studs di semua sisi (gaya lama)

-- SUARA AYAM
local SOUND_IDS = {
	"rbxassetid://87322318330216",
}
local SOUND_VOLUME = 0.6
local SOUND_MAX_DISTANCE = 60   -- sejauh apa suara terdengar
local CLUCK_INTERVAL_MIN = 4    -- jeda suara tercepat (detik)
local CLUCK_INTERVAL_MAX = 14   -- jeda suara terlama (detik)

-- Warna
local FEATHERS = {
	Color3.fromRGB(245, 245, 245), -- putih
	Color3.fromRGB(180, 110, 60),  -- coklat
	Color3.fromRGB(235, 200, 140), -- krem
	Color3.fromRGB(45, 40, 40),    -- hitam
}
local BEAK = Color3.fromRGB(255, 170, 30)
local LEG = Color3.fromRGB(255, 170, 30)
local RED = Color3.fromRGB(215, 35, 35)
local EYE = Color3.fromRGB(15, 15, 15)
-- ======================================================

local S = CHICKEN_SCALE
local V = Vector3.new
local SURFACES = {"TopSurface", "BottomSurface", "LeftSurface", "RightSurface", "FrontSurface", "BackSurface"}

local function rand(a, b)
	return a + math.random() * (b - a)
end

local folder = Instance.new("Folder")
folder.Name = "ChickenFarm"
folder.Parent = workspace

local function makePart(size, color)
	local p = Instance.new("Part")
	p.Size = size * S
	p.Color = color
	p.Material = Enum.Material.Plastic
	p.Anchored = true
	p.CanCollide = false
	p.CanTouch = false
	p.CanQuery = false
	p.CastShadow = false
	for _, s in ipairs(SURFACES) do
		if STUDS_ALL_SIDES or s == "TopSurface" then
			p[s] = Enum.SurfaceType.Studs
		else
			p[s] = Enum.SurfaceType.Smooth
		end
	end
	p.Parent = folder
	return p
end

-- ============ PUSAT / TITIK KUMPUL (boleh banyak) ============
local centers = {}
if CENTER_PART_NAME ~= "" then
	workspace:WaitForChild(CENTER_PART_NAME, 5)
	task.wait(1) -- beri waktu semua Part ter-load
	for _, inst in ipairs(workspace:GetDescendants()) do
		if inst.Name == CENTER_PART_NAME and inst:IsA("BasePart") then
			table.insert(centers, {
				pos = inst.Position,
				ground = inst.Position.Y + inst.Size.Y / 2,
			})
		end
	end
end
if #centers == 0 then
	warn("[ChickenFarm] Part '" .. CENTER_PART_NAME .. "' tidak ditemukan, memakai CENTER_POSITION")
	table.insert(centers, {pos = CENTER_POSITION, ground = CENTER_POSITION.Y})
end

-- ============ RAYCAST KE TANAH ============
local player = Players.LocalPlayer
local rayParams = RaycastParams.new()
rayParams.FilterType = Enum.RaycastFilterType.Exclude

local function updateFilter()
	local list = {folder}
	if player.Character then
		table.insert(list, player.Character)
	end
	rayParams.FilterDescendantsInstances = list
end
updateFilter()
player.CharacterAdded:Connect(updateFilter)

local function groundY(x, z, fallback)
	local origin = V(x, fallback + 30, z)
	local result = workspace:Raycast(origin, V(0, -200, 0), rayParams)
	if result then
		return result.Position.Y
	end
	return fallback
end

-- ============ MEMBUAT AYAM ============
-- Tiap part punya "group" (kerangka gerak) dan posisi relatif.
-- Ayam menghadap ke arah -Z.
local HIP_Y = -0.5
local ROOT_HEIGHT = 1.4 -- tinggi pusat badan dari tanah (sebelum scale)

local function buildChicken(index)
	local body = FEATHERS[(index - 1) % #FEATHERS + 1]
	local c = {parts = {}}

	local function add(group, size, rel, color)
		local p = makePart(size, color)
		table.insert(c.parts, {
			part = p,
			group = group,
			rel = CFrame.new(rel * S),
		})
	end

	-- badan
	add("root", V(1.2, 1.1, 1.6), V(0, 0, 0), body)
	add("root", V(1, 0.5, 0.8), V(0, 0.1, -0.6), body) -- dada

	-- kepala
	add("head", V(0.7, 0.7, 0.7), V(0, 0, 0), body)
	add("head", V(0.3, 0.2, 0.4), V(0, -0.05, -0.5), BEAK)  -- paruh
	add("head", V(0.2, 0.3, 0.5), V(0, 0.45, 0), RED)       -- jengger
	add("head", V(0.15, 0.25, 0.15), V(0, -0.4, -0.3), RED) -- gelambir
	add("head", V(0.1, 0.15, 0.15), V(-0.36, 0.1, -0.15), EYE) -- mata kiri
	add("head", V(0.1, 0.15, 0.15), V(0.36, 0.1, -0.15), EYE)  -- mata kanan

	-- ekor
	add("tail", V(0.3, 0.9, 0.7), V(0, 0.3, 0.1), body)
	add("tail", V(0.2, 0.7, 0.5), V(0, 0.45, 0.35), body)

	-- sayap
	add("wingL", V(0.12, 0.7, 1.1), V(-0.06, -0.3, 0), body)
	add("wingR", V(0.12, 0.7, 1.1), V(0.06, -0.3, 0), body)

	-- kaki
	add("legL", V(0.2, 0.8, 0.2), V(0, -0.4, 0), LEG)
	add("legL", V(0.4, 0.1, 0.6), V(0, -0.85, -0.15), LEG)
	add("legR", V(0.2, 0.8, 0.2), V(0, -0.4, 0), LEG)
	add("legR", V(0.4, 0.1, 0.6), V(0, -0.85, -0.15), LEG)

	-- titik kumpul milik ayam ini (dibagi rata)
	c.home = centers[(index - 1) % #centers + 1]

	-- posisi awal
	local a = rand(0, math.pi * 2)
	local r = math.sqrt(math.random()) * ROAM_RADIUS
	c.x = c.home.pos.X + math.cos(a) * r
	c.z = c.home.pos.Z + math.sin(a) * r
	c.y = nil
	c.yaw = rand(0, math.pi * 2)

	c.speed = rand(SPEED_MIN, SPEED_MAX)
	c.state = "idle"
	c.stateEnd = rand(0.5, 3)
	c.tx, c.tz = c.x, c.z
	c.legPhase = rand(0, math.pi * 2)
	c.peckPhase = rand(0, math.pi * 2)
	c.walkAmp = 0
	c.peckAmp = 0
	c.flutter = 0
	c.flutterPhase = 0
	c.nextCluck = rand(1, CLUCK_INTERVAL_MAX)

	-- suara
	c.sounds = {}
	local bodyPart = c.parts[1].part
	for _, id in ipairs(SOUND_IDS) do
		if id ~= "" then
			local s = Instance.new("Sound")
			s.SoundId = id
			s.Volume = SOUND_VOLUME
			s.RollOffMode = Enum.RollOffMode.Linear
			s.RollOffMinDistance = 5
			s.RollOffMaxDistance = SOUND_MAX_DISTANCE
			s.Parent = bodyPart
			table.insert(c.sounds, s)
		end
	end

	return c
end

local chickens = {}
for i = 1, CHICKEN_COUNT do
	table.insert(chickens, buildChicken(i))
end

local moveParts = {}
for _, c in ipairs(chickens) do
	for _, item in ipairs(c.parts) do
		table.insert(moveParts, item.part)
	end
end
local moveCFs = {}

-- ============ PERILAKU ============
local function pickTarget(c)
	local a = rand(0, math.pi * 2)
	local r = math.sqrt(math.random()) * ROAM_RADIUS
	c.tx = c.home.pos.X + math.cos(a) * r
	c.tz = c.home.pos.Z + math.sin(a) * r
end

local function angleDiff(a, b)
	local d = (a - b + math.pi) % (math.pi * 2) - math.pi
	return d
end

-- ============ ANIMASI ============
local t = 0

RunService.Heartbeat:Connect(function(dt)
	t = t + dt
	local n = 0

	for _, c in ipairs(chickens) do
		-- ---- keputusan perilaku ----
		if c.state == "walk" then
			local dx, dz = c.tx - c.x, c.tz - c.z
			local dist = math.sqrt(dx * dx + dz * dz)
			if dist < 1 then
				if math.random() < 0.7 then
					c.state = "peck"
				else
					c.state = "idle"
				end
				c.stateEnd = t + rand(2, 5)
			end
		elseif t >= c.stateEnd then
			c.state = "walk"
			pickTarget(c)
		end

		-- ---- gerak ----
		local moving = 0
		if c.state == "walk" then
			local dx, dz = c.tx - c.x, c.tz - c.z
			local desiredYaw = math.atan2(-dx, -dz)
			local diff = angleDiff(desiredYaw, c.yaw)
			local maxTurn = TURN_SPEED * dt
			c.yaw = c.yaw + math.clamp(diff, -maxTurn, maxTurn)
			if math.abs(diff) < 1.0 then
				local step = c.speed * dt
				c.x = c.x - math.sin(c.yaw) * step
				c.z = c.z - math.cos(c.yaw) * step
				moving = 1
			end
		end

		c.walkAmp = c.walkAmp + (moving - c.walkAmp) * math.min(1, dt * 8)
		local pecking = 0
		if c.state == "peck" then
			pecking = 1
		end
		c.peckAmp = c.peckAmp + (pecking - c.peckAmp) * math.min(1, dt * 6)

		c.legPhase = c.legPhase + dt * c.speed * 2.2 * c.walkAmp
		c.peckPhase = c.peckPhase + dt * 9

		-- ---- suara + kepak sayap saat berkokok ----
		if t >= c.nextCluck then
			c.nextCluck = t + rand(CLUCK_INTERVAL_MIN, CLUCK_INTERVAL_MAX)
			if #c.sounds > 0 then
				local s = c.sounds[math.random(1, #c.sounds)]
				s.PlaybackSpeed = rand(0.9, 1.2)
				s:Play()
			end
			if math.random() < 0.5 then
				c.flutter = 0.6
			end
		end
		if c.flutter > 0 then
			c.flutter = c.flutter - dt
			c.flutterPhase = c.flutterPhase + dt * 35
		end
		local fl = 0
		if c.flutter > 0 then
			fl = 1
		end

		-- ---- tinggi tanah ----
		local gy = groundY(c.x, c.z, c.home.ground)
		if not c.y then
			c.y = gy
		end
		c.y = c.y + (gy - c.y) * math.min(1, dt * 10)

		local bob = math.abs(math.sin(c.legPhase)) * 0.1 * S * c.walkAmp
		local rootPos = V(c.x, c.y + ROOT_HEIGHT * S + bob, c.z)
		local rootCF = CFrame.new(rootPos) * CFrame.Angles(0, c.yaw, 0)

		-- ---- kepala ----
		local peckPitch = c.peckAmp * (0.2 + 0.8 * math.max(0, math.sin(c.peckPhase)))
		local headBobZ = math.sin(c.legPhase * 2) * 0.12 * S * c.walkAmp
		local headCF = rootCF
			* CFrame.new(0, 0.3 * S, -0.7 * S + headBobZ)
			* CFrame.Angles(-peckPitch * 1.1, 0, 0)
			* CFrame.new(0, 0.4 * S, -0.1 * S)

		-- ---- ekor ----
		local tailCF = rootCF
			* CFrame.new(0, 0.2 * S, 0.8 * S)
			* CFrame.Angles(0.4 + math.sin(t * 2 + c.legPhase) * 0.03, 0, 0)

		-- ---- sayap ----
		local wingAng = 0.12 + math.sin(c.legPhase * 2) * 0.05 * c.walkAmp
		wingAng = wingAng + math.sin(c.flutterPhase) * 0.8 * fl + 0.5 * fl
		local wingLCF = rootCF * CFrame.new(-0.62 * S, 0.25 * S, 0) * CFrame.Angles(0, 0, -wingAng)
		local wingRCF = rootCF * CFrame.new(0.62 * S, 0.25 * S, 0) * CFrame.Angles(0, 0, wingAng)

		-- ---- kaki ----
		local swing = math.sin(c.legPhase) * 0.7 * c.walkAmp
		local legLCF = rootCF * CFrame.new(-0.3 * S, HIP_Y * S, 0.1 * S) * CFrame.Angles(swing, 0, 0)
		local legRCF = rootCF * CFrame.new(0.3 * S, HIP_Y * S, 0.1 * S) * CFrame.Angles(-swing, 0, 0)

		local frames = {
			root = rootCF,
			head = headCF,
			tail = tailCF,
			wingL = wingLCF,
			wingR = wingRCF,
			legL = legLCF,
			legR = legRCF,
		}

		for _, item in ipairs(c.parts) do
			n = n + 1
			moveCFs[n] = frames[item.group] * item.rel
		end
	end

	workspace:BulkMoveTo(moveParts, moveCFs, Enum.BulkMoveMode.FireCFrameChanged)
end)