CobraSpawner v4 (anti lag: tidur saat tidak ada pemain, update 30x/detik) -- Cobra kepala 3 merah-hitam-emas (stud), hanya hidup & mengejar di Part "SnakePlace".
-- CobraSpawner v4 (anti lag: tidur saat tidak ada pemain, update 30x/detik) (Script BUKAN LocalScript, di ServerScriptService)
-- Cobra kepala 3 merah-hitam-emas (stud), hanya hidup & mengejar di Part "SnakePlace".
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
-- ============ PENGATURAN ============
local PLACE_NAME = "SnakePlace"
local SCALE = 1.5 -- UKURAN COBRA (1 = normal, 2 = dua kali lebih besar)
local DETECT_RANGE = 90 * SCALE
local ATTACK_RANGE = 13 * SCALE
local BITE_RADIUS = 8 * SCALE
local DAMAGE = 20
local POISON = 4 -- racun per detik selama 3 detik (0 = tanpa racun)
local COOLDOWN = 2
local BITE_TIME = 0.9
local PATROL_SPEED = 7 * SCALE
local CHASE_SPEED = 18 * SCALE
local TURN_SPEED = 3
local EDGE = 5 * SCALE -- jarak aman dari tepi part
local SPACING = 2.8 * SCALE
local BODY, TAIL = 8, 5
local ACTIVE_RANGE = 300 -- cobra "tidur" kalau tidak ada pemain sejauh ini dari tepi SnakePlace
local UPDATE_STEP = 1 / 30 -- update 30x per detik (hemat server & jaringan)
-- ====================================
local V, C, A = Vector3.new, CFrame.new, CFrame.Angles
local function col(r, g, b) return Color3.fromRGB(r, g, b) end
local RED, DRED, BLACK = col(205, 35, 30), col(120, 18, 22), col(22, 22, 24)
local GOLD, YELLOW = col(228, 172, 40), col(255, 235, 40)
local WHITE, PINK = col(255, 255, 255), col(235, 60, 100)
local PATTERN = {RED, RED, BLACK}
local SURF = {"TopSurface", "BottomSurface", "LeftSurface", "RightSurface", "FrontSurface", "BackSurface"}
local FAN = {-0.9, 0, 0.9}
local N = BODY + TAIL
local place
for _ = 1, 30 do
local f = workspace:FindFirstChild(PLACE_NAME, true)
if f and f:IsA("BasePart") then
place = f
break
end
task.wait(0.5)
end
if not place then
warn("[Cobra] Part '" .. PLACE_NAME .. "' tidak ditemukan (harus Part).")
return
end
local topY = place.Size.Y / 2
local function smooth(a)
a = math.clamp(a, 0, 1)
return a * a * (3 - 2 * a)
end
local function wOf(i)
if i <= BODY then
return (4.6 - (i - 1) * 0.22) * SCALE
end
return (2.5 - (i - BODY - 1) * 0.4) * SCALE
end
local function build()
local model = Instance.new("Model")
model.Name = "Cobra"
local S = {G = {}, list = {}, parts = {}, plist = {}, cfs = {}, sub = {}, tips = {}, H = {}, seg = {},
L = V(0, 0, 0), hd = math.random() * 6.28, phase = 0, w = 0.3, lg = 0}
local function grp(name, pivot, parent)
S.G[name] = {pivot = pivot, parent = parent and S.G[parent], rot = C()}
S.list[#S.list + 1] = S.G[name]
end
local function add(gn, name, size, cf, color)
local p = Instance.new("Part")
p.Name = name
p.Size = gn and size * SCALE or size
p.Color = color
p.Anchored = true
p.CanCollide = false
p.CanTouch = false
p.CanQuery = false
p.CastShadow = false
for _, s in ipairs(SURF) do
p[s] = Enum.SurfaceType.Studs
end
p.Parent = model
S.parts[#S.parts + 1] = {g = gn and S.G[gn], cf = cf}
S.plist[#S.plist + 1] = p
return p
end
-- badan + ekor: balok belang merah/hitam, duri emas, rel emas di sisi
for i = 1, N do
local w = wOf(i)
local h = w * 0.8
local len = SPACING + 0.8
add(nil, "Body", V(w, h, len), C(), PATTERN[(i - 1) % 3 + 1])
S.sub[i] = {{#S.parts, C()}}
if i % 2 == 0 then
add(nil, "Spike", V(w * 0.45, 0.9, 1.4), C(), GOLD)
S.sub[i][#S.sub[i] + 1] = {#S.parts, C(0, h / 2 + 0.4, 0)}
end
for _, s in ipairs({-1, 1}) do
add(nil, "Rail", V(0.5, h * 0.3, len - 0.2), C(), GOLD)
S.sub[i][#S.sub[i] + 1] = {#S.parts, C(s * w / 2, -h * 0.22, 0)}
end
end
-- leher utama tebal (bertingkat) dengan dada emas
grp("Front", V(0, 0, 0))
grp("N1", V(0, 1, 0), "Front")
grp("N2", V(0, 3.5, 0), "N1")
grp("N3", V(0, 5.8, 0), "N2")
local TR = {{"N1", 5.4, 2.6, 4.2, 2.3, -0.2, DRED}, {"N2", 5, 2.4, 4, 4.7, -0.5, RED}, {"N3", 4.6, 2.4, 3.6, 7, -0.8, DRED}}
for _, r in ipairs(TR) do
add(r[1], "Neck", V(r[2], r[3], r[4]), C(0, r[5], r[6]), r[7])
add(r[1], "Belly", V(2.6, r[3] * 0.9, 0.6), C(0, r[5], r[6] - r[4] / 2 - 0.2), GOLD)
add(r[1], "Band", V(r[2] + 0.3, 0.5, r[4] + 0.3), C(0, r[5] + r[3] / 2 - 0.1, r[6]), BLACK)
end
-- 3 kepala: kotak lebar, hood kipas besar, tiap kepala beda arah
local HW = {5.2, 7.2, 8.2, 7.4, 5.4}
local HC = {DRED, BLACK, DRED, BLACK, DRED}
for k = 1, 3 do
local hub = V(-FAN[k] * 2.9, k == 2 and 7.9 or 7.2, -1.2)
local function hv(cf) return C(hub) * cf end
local g, j, tg, o = "H" .. k, "J" .. k, "T" .. k, "O" .. k
grp(g, hub, "N3")
grp(j, hub + V(0, 0.4, -4.4), g)
grp(tg, hub + V(0, 0.5, -8.5), g)
grp(o, hub + V(0, 0.5, -2.6), g)
-- hood
for r = 1, 5 do
local y = 0.6 + (r - 1) * 1.15
add(o, "Hood", V(HW[r], 1.15, 0.8), hv(C(0, y, -2.6)), HC[r])
for _, s in ipairs({-1, 1}) do
add(o, "Rim", V(0.9, 1.2, 1), hv(C(s * (HW[r] / 2 + 0.45), y, -2.6)), GOLD)
end
end
add(o, "Crown", V(2.4, 0.8, 1), hv(C(0, 6.4, -2.6)), GOLD)
-- leher kepala + dada emas
add(g, "HeadNeck", V(4, 3.4, 3.4), hv(C(0, 0.8, -1.7)), DRED)
add(g, "Throat", V(2.4, 2.2, 0.5), hv(C(0, 0.2, -3.65)), GOLD)
-- kepala kotak bertingkat
S.tips[k] = add(g, "Upper", V(4.6, 2, 3.2), hv(C(0, 1.5, -5.6)), RED)
add(g, "Snout", V(3, 1.6, 1.6), hv(C(0, 1.3, -7.8)), RED)
add(g, "Brow", V(4.6, 0.5, 2.4), hv(C(0, 2.75, -5.4)), BLACK)
for _, s in ipairs({-1, 1}) do
add(g, "Crest", V(1, 0.7, 1), hv(C(s * 1.3, 3.35, -4.6)), GOLD)
local eye = add(g, "Eye", V(0.8, 0.9, 0.5), hv(C(s * 1.9, 1.8, -7.4)), YELLOW)
eye.Material = Enum.Material.Neon
add(g, "Fang", V(0.5, 1.5, 0.5), hv(C(s, -0.2, -8)), WHITE)
add(tg, "Prong", V(0.25, 0.25, 1), hv(C(s * 0.3, 0.5, -10.9) * A(0, -s * 0.4, 0)), PINK)
end
-- rahang bawah + dagu emas + lidah bercabang
add(j, "Jaw", V(3.6, 0.8, 3.6), hv(C(0, 0.1, -6.2)), DRED)
add(j, "Chin", V(2.6, 0.5, 2.8), hv(C(0, -0.55, -6.3)), GOLD)
add(tg, "Tongue", V(0.5, 0.25, 2.8), hv(C(0, 0.5, -9)), PINK)
S.H[k] = {y = FAN[k], p = 0, j = 0, t = 1, f = 0, last = os.clock() + (k - 1) * 0.6}
end
for i = 1, N do
S.seg[i] = V(0, 0, 0) - V(math.cos(S.hd), 0, math.sin(S.hd)) * SPACING * (i - 1)
end
model.Parent = workspace
return S
end
local S = build()
local function poison(hum)
if POISON <= 0 then
return
end
task.spawn(function()
for _ = 1, 3 do
task.wait(1)
if hum.Health <= 0 then
break
end
hum:TakeDamage(POISON)
end
end)
end
local t, acc = 0, 0
RunService.Heartbeat:Connect(function(dt)
acc = acc + dt
if acc < UPDATE_STEP - 0.003 then
return
end
dt, acc = math.min(acc, 0.1), 0
t = t + dt
local pcf = place.CFrame
local hx = math.max(3, place.Size.X / 2 - EDGE)
local hz = math.max(3, place.Size.Z / 2 - EDGE)
-- pemain terdekat yang berada di atas SnakePlace
local tgt, tp, d
local near = false
for _, plr in ipairs(Players:GetPlayers()) do
local ch = plr.Character
local hum = ch and ch:FindFirstChildOfClass("Humanoid")
local hrp = ch and ch:FindFirstChild("HumanoidRootPart")
if hum and hrp and hum.Health > 0 then
local lp = pcf:PointToObjectSpace(hrp.Position)
local ex = math.max(math.abs(lp.X) - place.Size.X / 2, 0)
local ez = math.max(math.abs(lp.Z) - place.Size.Z / 2, 0)
if ex * ex + ez * ez <= ACTIVE_RANGE * ACTIVE_RANGE then
near = true
end
if math.abs(lp.X) <= place.Size.X / 2 + 3 and math.abs(lp.Z) <= place.Size.Z / 2 + 3
and lp.Y > topY - 4 and lp.Y < topY + 40 then
local dd = V(lp.X - S.L.X, 0, lp.Z - S.L.Z).Magnitude
if dd <= DETECT_RANGE and (not d or dd < d) then
tgt, tp, d = hrp, lp, dd
end
end
end
end
if not near then
return -- tidak ada pemain di dekat: cobra tidur (tidak ada beban)
end
-- gerak
local speed, desired = PATROL_SPEED, S.hd
if tgt then
desired = math.atan2(tp.Z - S.L.Z, tp.X - S.L.X)
speed = d <= ATTACK_RANGE - 3 and 0 or CHASE_SPEED
else
if not S.pt or (S.pt - S.L).Magnitude < 4 or os.clock() > (S.nextPt or 0) then
S.pt = V((math.random() * 2 - 1) * hx, 0, (math.random() * 2 - 1) * hz)
S.nextPt = os.clock() + math.random(5, 9)
end
desired = math.atan2(S.pt.Z - S.L.Z, S.pt.X - S.L.X)
end
local diff = (desired - S.hd + math.pi) % (2 * math.pi) - math.pi
S.hd = S.hd + math.clamp(diff, -TURN_SPEED * dt, TURN_SPEED * dt)
local f = V(math.cos(S.hd), 0, math.sin(S.hd))
local nl = S.L + f * speed * dt
local cx, cz = math.clamp(nl.X, -hx, hx), math.clamp(nl.Z, -hz, hz)
if cx ~= nl.X or cz ~= nl.Z then
S.pt = nil
end
S.L = V(cx, 0, cz)
S.phase = S.phase + speed * dt * 0.7 + dt * (speed > 0 and 2 or 0.8)
S.w = S.w + ((speed > 0 and 1 or 0.25) - S.w) * math.min(1, dt * 5)
-- badan mengikuti kepala
S.seg[1] = S.L
for i = 2, N do
local v = S.seg[i - 1] - S.seg[i]
local m = v.Magnitude
if m > SPACING then
S.seg[i] = S.seg[i - 1] - v / m * SPACING
end
end
-- gelombang melata
local wp = {}
for i = 1, N do
local fw = f
if i > 1 then
local v = S.seg[i - 1] - S.seg[i]
if v.Magnitude > 0.01 then
fw = v.Unit
end
end
local perp = V(-fw.Z, 0, fw.X)
local amp = math.sin(S.phase - i * 0.85) * 1.3 * SCALE * S.w * math.min(1, (i - 1) / 3) * (0.5 + i / N)
wp[i] = S.seg[i] + perp * amp
end
for i = 1, N do
local dv = i == 1 and (wp[1] - wp[2]) or (wp[i - 1] - wp[i])
if dv.Magnitude < 0.01 then
dv = f
end
local pos = V(wp[i].X, topY + wOf(i) * 0.4, wp[i].Z)
local base = pcf * CFrame.lookAt(pos, pos + dv.Unit)
for _, e in ipairs(S.sub[i]) do
S.parts[e[1]].cf = base * e[2]
end
end
-- kerangka depan (leher + kepala)
local f1 = wp[1] - wp[2]
f1 = f1.Magnitude > 0.01 and f1.Unit or f
local fp = wp[1] + f1 * S.lg * SCALE
local fpos = V(fp.X, topY, fp.Z)
local frontLocal = CFrame.lookAt(fpos, fpos + f1)
local Pf, dF = V(0, 0, -1), 0
if tgt then
Pf = frontLocal:PointToObjectSpace(tp)
dF = V(Pf.X, 0, Pf.Z).Magnitude / SCALE
end
local aim = tgt and math.clamp(math.atan2(-Pf.X, -Pf.Z), -1.2, 1.2) or 0
-- 3 kepala: idle, waspada, mematuk bergantian
local lgT, anyBite = 0, false
for k = 1, 3 do
if S.H[k].bite then
anyBite = true
end
end
for k = 1, 3 do
local h = S.H[k]
if tgt and d <= ATTACK_RANGE and not anyBite and not h.bite and os.clock() - h.last >= COOLDOWN then
h.bite, h.hit, h.last = 0, false, os.clock()
anyBite = true
end
local fan = FAN[k]
local yaw = fan
local pitch = (k == 2 and 0.12 or 0.02) + math.sin(t * 1.3 + k) * 0.05
local jaw = -0.1 - 0.06 * math.sin(t * 2 + k)
local ton, flare = 1, math.sin(t * 1.5 + k) * 0.03
if tgt then
yaw, pitch, jaw, flare = fan * 0.5 + aim * 0.7, -0.1, -0.35 + 0.1 * math.sin(t * 9 + k), 0.2
end
if h.bite then
local p = h.bite / BITE_TIME
local ps = -math.clamp(math.atan2(5, math.max(dF - 4.5, 1)), 0.35, 1.3)
yaw, ton, flare = fan * 0.2 + aim, 0, 0.3
if p < 0.3 then
local a = smooth(p / 0.3)
lgT, pitch, jaw = -1.5 * a, 0.5 * a, -0.9 * a
elseif p < 0.5 then
local a = smooth((p - 0.3) / 0.2)
lgT, pitch, jaw = -1.5 + 5.5 * a, 0.5 + (ps - 0.5) * a, -0.9
elseif p < 0.6 then
lgT, pitch, jaw = 4, ps, 0
else
local a = smooth((p - 0.6) / 0.4)
lgT, pitch, jaw = 4 * (1 - a), ps * (1 - a), 0
end
if p >= 0.52 and not h.hit then
h.hit = true
for _, plr in ipairs(Players:GetPlayers()) do
local ch = plr.Character
local hum = ch and ch:FindFirstChildOfClass("Humanoid")
local hrp = ch and ch:FindFirstChild("HumanoidRootPart")
if hum and hrp and hum.Health > 0 and (hrp.Position - S.tips[k].Position).Magnitude <= BITE_RADIUS then
hum:TakeDamage(DAMAGE)
poison(hum)
end
end
end
h.bite = h.bite + dt
if h.bite >= BITE_TIME then
h.bite = nil
end
end
h.y = h.y + (yaw - h.y) * math.min(1, dt * (h.bite and 20 or 8))
h.p = h.p + (pitch - h.p) * math.min(1, dt * (h.bite and 18 or 8))
h.j = h.j + (jaw - h.j) * math.min(1, dt * 20)
h.t = h.t + (ton - h.t) * math.min(1, dt * 15)
h.f = h.f + (flare - h.f) * math.min(1, dt * 6)
S.G["H" .. k].rot = A(0, h.y, 0) * A(h.p, 0, 0)
S.G["J" .. k].rot = A(h.j, 0, 0)
S.G["O" .. k].rot = A(h.f, 0, 0)
S.G["T" .. k].rot = C(0, 0, -h.t * (0.5 + 0.5 * math.sin(t * 14 + k * 2.1)) * 1.4)
end
S.lg = S.lg + (lgT - S.lg) * math.min(1, dt * 12)
local lean = S.lg * 0.07
local G = S.G
G.N1.rot = A(-lean * 0.3 + math.sin(t * 1.1) * 0.03, math.sin(t * 0.8) * 0.08, math.sin(t * 0.6) * 0.04)
G.N2.rot = A(-lean * 0.35 + math.sin(t * 1.3 + 1) * 0.04, math.sin(t * 0.9 + 1) * 0.1, math.sin(t * 0.7) * 0.04)
G.N3.rot = A(-lean * 0.35 + math.sin(t * 1.5 + 2) * 0.04, math.sin(t * 1.1 + 2) * 0.1, 0)
-- hitung semua posisi
local frontCF = pcf * frontLocal
for _, g in ipairs(S.list) do
if g.parent then
g.world = g.parent.world * C(g.pivot - g.parent.pivot) * g.rot
else
g.world = frontCF * C(g.pivot) * g.rot
end
end
for i, e in ipairs(S.parts) do
if e.g then
local rel = frontCF:ToObjectSpace(e.g.world * C(-e.g.pivot) * e.cf)
S.cfs[i] = frontCF * C(rel.Position * SCALE) * (rel - rel.Position)
else
S.cfs[i] = e.cf
end
end
workspace:BulkMoveTo(S.plist, S.cfs, Enum.BulkMoveMode.FireCFrameChanged)
end)