← KEMBALI KE SCRIPT LIBRARY

Trex

UmumVersi 1.0Update 24/11/2026

- TRexSpawner v4 (T-Rex hanya beraksi di jalan T-rexPlace, kebal kill part) -- Script (BUKAN LocalScript) di ServerScriptService. -- T-Rex hanya muncul di Part bernama "T-rexPlace".

Cara penggunaan
Full Tutorial di YouTube iBalDBK
Lokasi pemasangan
Script (BUKAN LocalScript) di ServerScriptService
Script
-- TRexSpawner v4 (T-Rex hanya beraksi di jalan T-rexPlace, kebal kill part)
-- Script (BUKAN LocalScript) di ServerScriptService.
-- T-Rex hanya muncul di Part bernama "T-rexPlace".

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

-- ===================== PENGATURAN =====================
local PLACE_NAME = "T-rexPlace"
local SCALE = 0.8
local MAX_HEALTH = 600
local RESPAWN_TIME = 15
local DETECT_RANGE = 90
local LOSE_RANGE = 140
local ATTACK_RANGE = 14
local DAMAGE = 25
local ATTACK_COOLDOWN = 1.6
local WALK_SPEED = 10
local RUN_SPEED = 26
local PATROL_RADIUS = 40
local STUDS_ALL_SIDES = true
local BITE_TIME = 0.8
local ROAR_TIME = 2
local ROAR_COOLDOWN = 20
local ROAR_SOUND_ID = 0
local CHASE_MARGIN = 25        -- T-Rex mulai/berhenti mengejar pemain sejauh ini di luar tepi jalan
local EDGE_MARGIN = 6          -- jarak aman T-Rex dari tepi jalan (supaya tidak jatuh)
local INVINCIBLE = true        -- true = T-Rex tidak bisa mati (kebal kill part)

local GREEN = Color3.fromRGB(36, 92, 50)
local DARK = Color3.fromRGB(20, 58, 34)
local SKIN = Color3.fromRGB(222, 190, 150)
local TEETH = Color3.fromRGB(255, 255, 255)
local CLAW = Color3.fromRGB(236, 230, 210)
local EYE = Color3.fromRGB(255, 196, 30)
-- ======================================================

local GROUND_OFFSET = 9.6 * SCALE
local ROOT_SIZE = 2
local SURFACES = {"TopSurface", "BottomSurface", "LeftSurface", "RightSurface", "FrontSurface", "BackSurface"}
local V = Vector3.new
local SIDES = {{"L", -1}, {"R", 1}}

local place = nil
local tries = 0
while place == nil and tries < 30 do
	local found = workspace:FindFirstChild(PLACE_NAME, true)
	if found and found:IsA("BasePart") then
		place = found
	else
		tries = tries + 1
		task.wait(0.5)
	end
end
if place == nil then
	warn("[TRex] Part '" .. PLACE_NAME .. "' tidak ditemukan (harus berupa Part, bukan Model/Folder).")
	return
end
print("[TRex] Part T-rexPlace ditemukan di", place.Position)

local function sc(cf)
	return CFrame.new(cf.Position * SCALE) * (cf - cf.Position)
end

local function smooth(a)
	a = math.clamp(a, 0, 1)
	return a * a * (3 - 2 * a)
end

-- ============ MEMBANGUN T-REX ============
local function buildTRex(spawnCFrame)
	local model = Instance.new("Model")
	model.Name = "TRex"

	local root = Instance.new("Part")
	root.Name = "HumanoidRootPart"
	root.Size = V(ROOT_SIZE, ROOT_SIZE, ROOT_SIZE)
	root.Transparency = 1
	root.CanCollide = true
	root.CanTouch = false
	root.CFrame = spawnCFrame
	root.Parent = model
	model.PrimaryPart = root

	local parts = {Root = root}
	local rels = {Root = CFrame.new()}
	local M = {}
	local B = {}

	local function add(name, size, cf, color, parent, joint)
		local rel = sc(cf)
		local p = Instance.new("Part")
		p.Name = name
		p.Size = size * SCALE
		p.Color = color
		p.Material = Enum.Material.Plastic
		p.CanCollide = false
		p.Massless = true
		p.CanTouch = false
		p.CFrame = spawnCFrame * rel
		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
		local jointPos = joint
		if jointPos == nil then
			jointPos = cf.Position
		end
		local jointCF = CFrame.new(jointPos * SCALE)
		local m = Instance.new("Motor6D")
		m.Name = name
		m.Part0 = parts[parent]
		m.Part1 = p
		m.C0 = rels[parent]:Inverse() * jointCF
		m.C1 = rel:Inverse() * jointCF
		m.Parent = p
		p.Parent = model
		parts[name] = p
		rels[name] = rel
		M[name] = m
		B[name] = m.C0
		return p
	end

	-- Badan
	add("Body", V(9, 7, 12), CFrame.new(0, 3, 1), GREEN, "Root", V(0, 0, 0))
	add("Belly", V(7, 1, 10), CFrame.new(0, -0.9, 0.5), SKIN, "Body")
	add("Back", V(5, 0.6, 9), CFrame.new(0, 6.7, 1.5), DARK, "Body")
	local spikeZ = {-1, 2, 5}
	for i = 1, #spikeZ do
		add("Spike" .. i, V(1, 1.5, 2), CFrame.new(0, 7.75, spikeZ[i]), DARK, "Body")
	end
	add("Chest", V(8, 8, 6), CFrame.new(0, 4.5, -5.5), GREEN, "Body", V(0, 3, -3))

	-- Tangan kecil + cakar
	for _, sd in ipairs(SIDES) do
		local n = sd[1]
		local s = sd[2]
		add("Arm" .. n, V(1.4, 3.5, 1.4), CFrame.new(4.9 * s, 4, -7), GREEN, "Chest", V(4.9 * s, 5.8, -7))
		local clawX = {-0.4, 0.4}
		for i = 1, 2 do
			add("Claw" .. n .. i, V(0.4, 0.4, 1.4), CFrame.new(4.9 * s + clawX[i], 2.4, -7.6), CLAW, "Arm" .. n)
		end
	end

	-- Leher, kepala, rahang
	add("Neck", V(4.6, 7, 4.6), CFrame.new(0, 10.65, -8) * CFrame.Angles(-0.45, 0, 0), GREEN, "Chest", V(0, 7.5, -6.5))
	add("Head", V(6.4, 6, 8), CFrame.new(0, 14.8, -12), GREEN, "Neck", V(0, 13.6, -9.6))
	add("Snout", V(5, 3.6, 6), CFrame.new(0, 13.6, -18.5), GREEN, "Head")
	add("Jaw", V(4.6, 1.6, 9.5), CFrame.new(0, 10.9, -16.5), SKIN, "Head", V(0, 10.9, -11.8))

	local ti = 0
	local upZ = {-20.5, -18.5, -16.5}
	local upX = {-2.2, 2.2}
	for a = 1, #upZ do
		for b = 1, #upX do
			ti = ti + 1
			add("TeethU" .. ti, V(0.7, 1.2, 0.7), CFrame.new(upX[b], 11.2, upZ[a]), TEETH, "Snout")
		end
	end
	local frontX = {-1, 1}
	for b = 1, #frontX do
		ti = ti + 1
		add("TeethU" .. ti, V(0.7, 1.2, 0.7), CFrame.new(frontX[b], 11.2, -21.2), TEETH, "Snout")
	end
	local lowZ = {-20, -18, -16}
	local lowX = {-1.4, 1.4}
	for a = 1, #lowZ do
		for b = 1, #lowX do
			ti = ti + 1
			add("TeethL" .. ti, V(0.6, 1, 0.6), CFrame.new(lowX[b], 12.2, lowZ[a]), TEETH, "Jaw")
		end
	end
	for _, sd in ipairs(SIDES) do
		local n = sd[1]
		local s = sd[2]
		add("Eye" .. n, V(1, 1.2, 1.2), CFrame.new(3.3 * s, 16, -12.5), EYE, "Head")
		add("Brow" .. n, V(1.2, 0.9, 3), CFrame.new(3.3 * s, 17.1, -12.5), DARK, "Head")
		add("Nostril" .. n, V(0.8, 0.8, 0.5), CFrame.new(1.2 * s, 14.6, -21.7), DARK, "Snout")
	end

	-- Ekor
	add("Tail1", V(6, 5.5, 8), CFrame.new(0, 3, 10.5), GREEN, "Body", V(0, 3, 6.5))
	add("Tail2", V(4, 4, 8), CFrame.new(0, 2.8, 18.5), GREEN, "Tail1", V(0, 2.9, 14.5))
	add("Tail3", V(2.6, 2.8, 8), CFrame.new(0, 2.6, 26.5), GREEN, "Tail2", V(0, 2.7, 22.5))
	add("Tail4", V(1.4, 1.4, 5), CFrame.new(0, 2.5, 33), DARK, "Tail3", V(0, 2.5, 30.5))

	-- Kaki
	for _, sd in ipairs(SIDES) do
		local n = sd[1]
		local s = sd[2]
		add("Thigh" .. n, V(4, 7, 5), CFrame.new(3.8 * s, -2.5, 1), GREEN, "Body", V(3.8 * s, 0.8, 1))
		add("Shin" .. n, V(2.6, 5.5, 2.8), CFrame.new(3.7 * s, -6.5, 1.6), DARK, "Thigh" .. n, V(3.7 * s, -4.5, 1.4))
		add("Foot" .. n, V(3.4, 1.2, 6.5), CFrame.new(3.7 * s, -9, -0.2), DARK, "Shin" .. n, V(3.7 * s, -8.6, 1.6))
		add("Toe" .. n, V(2.6, 0.9, 1.4), CFrame.new(3.7 * s, -9.15, -3.9), CLAW, "Foot" .. n)
	end

	-- Humanoid
	local hum = Instance.new("Humanoid")
	hum.MaxHealth = MAX_HEALTH
	hum.Health = MAX_HEALTH
	hum.HipHeight = GROUND_OFFSET - ROOT_SIZE / 2
	hum.WalkSpeed = WALK_SPEED
	hum.RequiresNeck = false
	hum.BreakJointsOnDeath = false
	hum.DisplayDistanceType = Enum.HumanoidDisplayDistanceType.None
	if INVINCIBLE then
		hum:SetStateEnabled(Enum.HumanoidStateType.Dead, false)
	end
	hum.Parent = model

	model.Parent = workspace
	pcall(function()
		root:SetNetworkOwner(nil)
	end)

	return model, hum, root, M, B
end

-- ============ ANIMASI ============
local function startAnimator(root, M, B, A)
	local t = 0
	local phase = 0
	local w = 0
	local CF = CFrame.Angles

	local function step(dt)
		if not root.Parent then
			return
		end
		t = t + dt

		-- Mati: rebah ke samping
		if A.dead then
			A.deadT = A.deadT + dt
			local a = smooth(A.deadT / 1.2)
			M.Body.C0 = B.Body * CFrame.new(0, -4.6 * SCALE * a, 0) * CF(0, 0, 1.4 * a)
			M.Neck.C0 = B.Neck * CF(-0.3 * a, 0, 0)
			M.Jaw.C0 = B.Jaw * CF(-0.5 * a, 0, 0)
			return
		end

		if A.bite then
			A.bite = A.bite + dt
			if A.bite >= BITE_TIME then
				A.bite = nil
			end
		end
		if A.roar then
			A.roar = A.roar + dt
			if A.roar >= ROAR_TIME then
				A.roar = nil
			end
		end

		local v = root.AssemblyLinearVelocity
		local speed = Vector3.new(v.X, 0, v.Z).Magnitude
		local target = 0
		if speed > 1 and not A.bite and not A.roar then
			target = 1
		end
		w = w + (target - w) * math.min(1, dt * 8)
		phase = phase + dt * speed * 0.32
		local run = math.clamp(speed / RUN_SPEED, 0, 1)
		local amp = (0.3 + 0.3 * run) * w

		local neckAdd = 0
		local headAdd = 0
		local jawAdd = -0.12 * w
		local chestAdd = 0

		if A.bite then
			local p = A.bite / BITE_TIME
			if p < 0.35 then
				local a = smooth(p / 0.35)
				neckAdd = 0.4 * a
				headAdd = 0.2 * a
				jawAdd = -0.9 * a
			elseif p < 0.55 then
				local a = (p - 0.35) / 0.2
				neckAdd = 0.4 - 0.95 * smooth(a)
				headAdd = 0.2 - 0.5 * smooth(a)
				jawAdd = -0.9 * (1 - smooth((a - 0.7) / 0.3))
			else
				local a = smooth((p - 0.55) / 0.45)
				neckAdd = -0.55 * (1 - a)
				headAdd = -0.3 * (1 - a)
				jawAdd = 0
			end
		end

		if A.roar then
			local p = A.roar / ROAR_TIME
			local a = math.clamp(math.sin(math.pi * p) * 3, 0, 1)
			neckAdd = 0.5 * a
			headAdd = 0.35 * a
			jawAdd = (-0.8 + math.sin(t * 25) * 0.08) * a
			chestAdd = 0.12 * a
		end

		-- Badan, dada, leher, kepala, rahang
		M.Body.C0 = B.Body * CFrame.new(0, math.abs(math.sin(phase)) * 0.45 * SCALE * w, 0) * CF(math.sin(phase) * 0.04 * w, 0, 0)
		M.Chest.C0 = B.Chest * CF(chestAdd + math.sin(t * 1.6) * 0.02, 0, 0)
		M.Neck.C0 = B.Neck * CF(neckAdd + math.sin(t * 1.6 + 0.5) * 0.03, 0, 0)
		M.Head.C0 = B.Head * CF(headAdd, math.sin(t * 0.5) * 0.2 * (1 - w), 0)
		M.Jaw.C0 = B.Jaw * CF(jawAdd, 0, 0)

		-- Ekor
		for i = 1, 4 do
			local ang = math.sin(phase - i * 0.7) * 0.16 * w + math.sin(t * 0.9 - i * 0.7) * 0.07 * (1 - w)
			M["Tail" .. i].C0 = B["Tail" .. i] * CF(0, ang, 0)
		end

		-- Tangan & kaki
		for _, sd in ipairs(SIDES) do
			local n = sd[1]
			local s = sd[2]
			M["Arm" .. n].C0 = B["Arm" .. n] * CF(math.sin(phase + s) * 0.3 * w + math.sin(t * 2 + s) * 0.05, 0, 0)

			local ph = phase
			if s == 1 then
				ph = phase + math.pi
			end
			local hip = math.sin(ph) * amp
			local knee = -math.max(0, math.cos(ph)) * amp * 1.5
			local foot = -(hip + knee) * 0.8
			M["Thigh" .. n].C0 = B["Thigh" .. n] * CF(hip, 0, 0)
			M["Shin" .. n].C0 = B["Shin" .. n] * CF(knee, 0, 0)
			M["Foot" .. n].C0 = B["Foot" .. n] * CF(foot, 0, 0)
		end
	end

	return RunService.Heartbeat:Connect(step)
end

-- ============ HELPER ============
local function inRoad(pos)
	local lp = place.CFrame:PointToObjectSpace(pos)
	return math.abs(lp.X) <= place.Size.X / 2 + CHASE_MARGIN
		and math.abs(lp.Z) <= place.Size.Z / 2 + CHASE_MARGIN
		and lp.Y > -30 and lp.Y < 100
end

local function clampToRoad(pos)
	local lp = place.CFrame:PointToObjectSpace(pos)
	local hx = math.max(0, place.Size.X / 2 - EDGE_MARGIN)
	local hz = math.max(0, place.Size.Z / 2 - EDGE_MARGIN)
	local cx = math.clamp(lp.X, -hx, hx)
	local cz = math.clamp(lp.Z, -hz, hz)
	return (place.CFrame * CFrame.new(cx, place.Size.Y / 2 + GROUND_OFFSET, cz)).Position
end

local function nearestPlayer(origin, range)
	local best = nil
	local bestDist = nil
	local bestHum = nil
	for _, plr in ipairs(Players:GetPlayers()) do
		local char = plr.Character
		local hum = char and char:FindFirstChildOfClass("Humanoid")
		local hrp = char and char:FindFirstChild("HumanoidRootPart")
		if hum and hrp and hum.Health > 0 and inRoad(hrp.Position) then
			local d = (hrp.Position - origin).Magnitude
			if d <= range and (bestDist == nil or d < bestDist) then
				best = hrp
				bestDist = d
				bestHum = hum
			end
		end
	end
	return best, bestDist, bestHum
end

local function flatDistance(a, b)
	return Vector3.new(a.X - b.X, 0, a.Z - b.Z).Magnitude
end

local function faceTarget(root, pos)
	local p = root.Position
	root.CFrame = CFrame.lookAt(p, Vector3.new(pos.X, p.Y, pos.Z))
end

local function playSound(parent, id)
	if id == 0 then
		return
	end
	local s = Instance.new("Sound")
	s.SoundId = "rbxassetid://" .. id
	s.RollOffMaxDistance = 300
	s.Volume = 2
	s.Parent = parent
	s:Play()
	Debris:AddItem(s, 6)
end

-- ============ SIKLUS HIDUP ============
local function spawnAndRun()
	local top = (place.CFrame * CFrame.new(0, place.Size.Y / 2 + GROUND_OFFSET + 1, 0)).Position
	print("[TRex] T-Rex muncul di", top)
	local model, hum, root, M, B = buildTRex(CFrame.new(top))
	local home = place.Position
	local A = {deadT = 0}
	local conn = startAnimator(root, M, B, A)

	hum.Died:Connect(function()
		root.Anchored = true
		A.bite = nil
		A.roar = nil
		A.dead = true
	end)

	local lastAttack = 0
	local lastRoar = -100
	local patrolTarget = nil
	local nextPatrol = 0
	local chasing = false

	while model.Parent and hum.Health > 0 do
		if INVINCIBLE then
			hum.Health = hum.MaxHealth
		end
		-- kalau keluar dari jalan atau jatuh, kembalikan ke jalan
		local lp = place.CFrame:PointToObjectSpace(root.Position)
		if lp.Y < -15 or math.abs(lp.X) > place.Size.X / 2 + 8 or math.abs(lp.Z) > place.Size.Z / 2 + 8 then
			root.AssemblyLinearVelocity = Vector3.new(0, 0, 0)
			root.CFrame = CFrame.new(top)
			hum:MoveTo(top)
		end

		local range = DETECT_RANGE
		if chasing then
			range = LOSE_RANGE
		end
		local target, _, tHum = nearestPlayer(root.Position, range)
		local didRoar = false

		if target then
			local first = not chasing
			chasing = true

			if first and os.clock() - lastRoar > ROAR_COOLDOWN then
				didRoar = true
				lastRoar = os.clock()
				hum.WalkSpeed = 0
				hum:MoveTo(root.Position)
				faceTarget(root, target.Position)
				A.roar = 0
				playSound(root, ROAR_SOUND_ID)
				while A.roar and hum.Health > 0 do
					task.wait(0.1)
				end
			end

			if not didRoar then
				local d = flatDistance(target.Position, root.Position)
				if d <= ATTACK_RANGE then
					hum.WalkSpeed = 0
					hum:MoveTo(root.Position)
					if not A.bite then
						faceTarget(root, target.Position)
					end
					if not A.bite and os.clock() - lastAttack >= ATTACK_COOLDOWN then
						lastAttack = os.clock()
						A.bite = 0
						task.delay(BITE_TIME * 0.55, function()
							if hum.Health > 0 and tHum.Health > 0 and target.Parent then
								if flatDistance(target.Position, root.Position) <= ATTACK_RANGE + 5 then
									tHum:TakeDamage(DAMAGE)
								end
							end
						end)
					end
				elseif not A.bite then
					hum.WalkSpeed = RUN_SPEED
					hum:MoveTo(clampToRoad(target.Position))
				end
			end
		else
			chasing = false
			hum.WalkSpeed = WALK_SPEED
			if patrolTarget == nil or os.clock() >= nextPatrol then
				local rx = (math.random() - 0.5) * math.min(place.Size.X * 0.6, PATROL_RADIUS * 2)
				local rz = (math.random() - 0.5) * math.min(place.Size.Z * 0.6, PATROL_RADIUS * 2)
				patrolTarget = (place.CFrame * CFrame.new(rx, 0, rz)).Position
				nextPatrol = os.clock() + math.random(4, 8)
			end
			hum:MoveTo(patrolTarget)
		end

		if not didRoar then
			task.wait(0.2)
		end
	end

	task.wait(4)
	conn:Disconnect()
	model:Destroy()
end

task.spawn(function()
	local running = true
	while running do
		spawnAndRun()
		if RESPAWN_TIME <= 0 then
			running = false
		else
			task.wait(RESPAWN_TIME)
		end
	end
end)