#when i kill one npc other npcs around the place die too
1 messages · Page 1 of 1 (latest)
can you upload it to my mind once you figure it out too thx
theres 2 scripts
1:
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local npc = script.Parent -- The NPC model
local humanoid = npc:WaitForChild("Humanoid")
local primaryPart = npc.PrimaryPart or npc:WaitForChild("HumanoidRootPart")
-- Load the animation
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://140135173569637"
local animationTrack = humanoid:LoadAnimation(animation)
animationTrack:Play()
-- Check distance to all players
RunService.Heartbeat:Connect(function()
local stopAnimation = false
for _, player in pairs(Players:GetPlayers()) do
local character = player.Character
if character and character:FindFirstChild("HumanoidRootPart") then
local distance = (primaryPart.Position - character.HumanoidRootPart.Position).Magnitude
if distance <= 15 then
stopAnimation = true
break
end
end
end
if stopAnimation then
if animationTrack.IsPlaying then
animationTrack:Stop()
end
else
if not animationTrack.IsPlaying then
animationTrack:Play()
end
end
end)
there's no humanoid:die() in this so it's probably the other one
second one:
(first half)
-- ===== SETTINGS =====
local DETECTION_RANGE = 15 -- how far NPC can see a player
local ATTACK_RANGE = 3 -- how close before attacking
local DAMAGE = 5 -- damage dealt per hit
local ATTACK_COOLDOWN = 1.5 -- seconds between attacks
local lastAttack = 0
-- ===== FUNCTIONS =====
-- Ragdoll the NPC
local function ragdoll()
-- Remove Motor6Ds to detach limbs
for _, part in pairs(npc:GetDescendants()) do
if part:IsA("Motor6D") then
part:Destroy()
end
end
-- Add BallSocketConstraints for realistic limb movement
for _, part in pairs(npc:GetChildren()) do
if part:IsA("BasePart") and part.Name ~= "HumanoidRootPart" then
local attach0 = Instance.new("Attachment", part)
local attach1 = Instance.new("Attachment", hrp)
local constraint = Instance.new("BallSocketConstraint")
constraint.Attachment0 = attach0
constraint.Attachment1 = attach1
constraint.Parent = part
end
end
-- Make parts collide and fall naturally
for _, part in pairs(npc:GetChildren()) do
if part:IsA("BasePart") then
part.CanCollide = true
part.Anchored = false
end
end
end
-- Find nearest player within detection range
local function getNearestPlayer()
local nearest = nil
local nearestDist = DETECTION_RANGE
for _, player in pairs(game.Players:GetPlayers()) do
if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
local targetHRP = player.Character.HumanoidRootPart
local dist = (targetHRP.Position - hrp.Position).Magnitude
if dist < nearestDist then
nearestDist = dist
nearest = player.Character
end
end
end
return nearest
end
(Second half)
-- Attack a target humanoid
local function attack(targetHumanoid)
if tick() - lastAttack > ATTACK_COOLDOWN then
lastAttack = tick()
if targetHumanoid and targetHumanoid.Health > 0 then
targetHumanoid:TakeDamage(DAMAGE)
end
end
end
-- ===== EVENTS =====
-- Handle death
humanoid.Died:Connect(function()
ragdoll()
end)
-- ===== MAIN LOOP =====
spawn(function()
while humanoid.Health > 0 do
local target = getNearestPlayer()
if target and target:FindFirstChild("Humanoid") and target:FindFirstChild("HumanoidRootPart") then
local targetHumanoid = target.Humanoid
local targetHRP = target.HumanoidRootPart
local dist = (targetHRP.Position - hrp.Position).Magnitude
if dist > ATTACK_RANGE then
-- Chase the player
humanoid:MoveTo(targetHRP.Position)
else
-- Attack when in range
attack(targetHumanoid)
end
end
task.wait(0.2)
end
end)
or tbh upload it as attachment, discord will do that with long messages
(i'm surprised you didn't see that...)
** You are now Level 1! **