-- SpiderScript.lua -- Cách dùng: Roblox Studio > ServerScriptService > Insert > Script, dán toàn bộ code này vào, bấm Play. -- Thân nhện = "PalletLightBrown", 8 chân nhện (mỗi chân 2 đoạn) = "LadderLightBrown". -- Nhện tự đi theo người chơi gần nhất, chân bước bằng IK nên bám được mặt đất gồ ghề. local RunService = game:GetService("RunService") local Players = game:GetService("Players") ---------------------------------------------------------------- CẤU HÌNH local CONFIG = { SpawnPosition = Vector3.new(0, 15, 0), Color = Color3.fromRGB(196, 164, 132), -- nâu nhạt BodySize = Vector3.new(4, 2, 5), Speed = 14, -- studs/giây StopDistance = 6, -- dừng lại khi cách người chơi bao nhiêu studs TurnSpeed = 6, BodyHeight = 4.5, -- độ cao thân so với mặt đất UpperLen = 5, -- đoạn chân trên LowerLen = 6, -- đoạn chân dưới LegThickness = 0.7, StepDistance = 3.5, -- chân lệch quá mức này thì bước StepTime = 0.18, StepHeight = 1.8, } ---------------------------------------------------------------- local spider = Instance.new("Model") spider.Name = "Spider" local function makePart(name, size, material) local p = Instance.new("Part") p.Name = name p.Size = size p.Color = CONFIG.Color p.Material = material p.Anchored = true p.CanCollide = false p.TopSurface = Enum.SurfaceType.Smooth p.BottomSurface = Enum.SurfaceType.Smooth p.Parent = spider return p end -- THÂN NHỆN local body = makePart("PalletLightBrown", CONFIG.BodySize, Enum.Material.WoodPlanks) body.CanCollide = true body.CFrame = CFrame.new(CONFIG.SpawnPosition) spider.PrimaryPart = body local rayParams = RaycastParams.new() rayParams.FilterType = Enum.RaycastFilterType.Exclude rayParams.FilterDescendantsInstances = { spider } local function groundAt(pos) local r = workspace:Raycast(pos + Vector3.new(0, 15, 0), Vector3.new(0, -80, 0), rayParams) return r and r.Position or Vector3.new(pos.X, 0, pos.Z) end -- CHÂN NHỆN (4 chân mỗi bên) local legs = {} local zs = { -1.8, -0.6, 0.6, 1.8 } for _, side in ipairs({ -1, 1 }) do for i, z in ipairs(zs) do local leg = { hip = Vector3.new(side * CONFIG.BodySize.X / 2, 0, z), rest = Vector3.new(side * 7, 0, z * 2.2), group = (i + (side == 1 and 0 or 1)) % 2, -- 2 nhóm chân bước xen kẽ upper = makePart("LadderLightBrown", Vector3.new(CONFIG.LegThickness, CONFIG.LegThickness, CONFIG.UpperLen), Enum.Material.Wood), lower = makePart("LadderLightBrown", Vector3.new(CONFIG.LegThickness, CONFIG.LegThickness, CONFIG.LowerLen), Enum.Material.Wood), stepping = false, t = 0, } leg.foot = groundAt(body.CFrame:PointToWorldSpace(leg.rest)) table.insert(legs, leg) end end -- Giải IK 2 đoạn: trả về vị trí đầu gối và bàn chân thực tế local function solveIK(hip, foot, l1, l2) local v = foot - hip local dir = v.Unit local d = math.clamp(v.Magnitude, math.abs(l1 - l2) + 0.1, l1 + l2 - 0.05) local a = (l1 * l1 - l2 * l2 + d * d) / (2 * d) local h = math.sqrt(math.max(l1 * l1 - a * a, 0)) local up = Vector3.yAxis - dir * dir.Y up = up.Magnitude > 0.01 and up.Unit or Vector3.zAxis return hip + dir * a + up * h, hip + dir * d end local function placeSegment(part, a, b) local len = (b - a).Magnitude if len < 0.05 then return end part.Size = Vector3.new(CONFIG.LegThickness, CONFIG.LegThickness, len) part.CFrame = CFrame.lookAt((a + b) / 2, b) end local function otherGroupStepping(group) for _, l in ipairs(legs) do if l.group ~= group and l.stepping then return true end end return false end local function nearestTarget(pos) local best, bestDist for _, plr in ipairs(Players:GetPlayers()) do local hrp = plr.Character and plr.Character:FindFirstChild("HumanoidRootPart") if hrp then local d = (hrp.Position - pos).Magnitude if not bestDist or d < bestDist then best, bestDist = hrp.Position, d end end end return best end spider.Parent = workspace RunService.Heartbeat:Connect(function(dt) local cf = body.CFrame local pos = cf.Position local moveDir = Vector3.zero -- Di chuyển + xoay về phía người chơi local target = nearestTarget(pos) if target then local flat = Vector3.new(target.X - pos.X, 0, target.Z - pos.Z) if flat.Magnitude > CONFIG.StopDistance then moveDir = flat.Unit pos = pos + moveDir * CONFIG.Speed * dt local look = CFrame.lookAt(pos, pos + moveDir).Rotation cf = CFrame.new(pos) * cf.Rotation:Lerp(look, math.min(1, dt * CONFIG.TurnSpeed)) end end -- Giữ thân ở độ cao cố định so với mặt đất local g = groundAt(cf.Position) cf = CFrame.new(cf.X, g.Y + CONFIG.BodyHeight, cf.Z) * cf.Rotation body.CFrame = cf -- Cập nhật từng chân for _, l in ipairs(legs) do local hip = cf:PointToWorldSpace(l.hip) local restW = groundAt(cf:PointToWorldSpace(l.rest)) if not l.stepping then local far = (restW - l.foot).Magnitude if (far > CONFIG.StepDistance and not otherGroupStepping(l.group)) or far > CONFIG.StepDistance * 2.5 then l.stepping = true l.t = 0 l.from = l.foot l.to = restW + moveDir * 2 end else l.t += dt / CONFIG.StepTime local a = math.min(l.t, 1) l.foot = l.from:Lerp(l.to, a) + Vector3.new(0, math.sin(a * math.pi) * CONFIG.StepHeight, 0) if l.t >= 1 then l.stepping = false l.foot = l.to end end local knee, foot = solveIK(hip, l.foot, CONFIG.UpperLen, CONFIG.LowerLen) placeSegment(l.upper, hip, knee) placeSegment(l.lower, knee, foot) end end)