So, I tried making a NPC script that attacks you if you get too close, and I managed to do it! There’s just one problem though… after the NPC kills me it just breaks and stays on the position it killed me and keeps punching the air, and even if I get on its damage range it doesnt damage, as well as if I push it (NPC) away it just walks back to where it killed me and continues punching the air.
I’ve tried to place
if char.Humanoid.Health <= 0 then
target = nil
script.StopAttackRemote:Fire()
else if char.Humanoid.Health > 1 or char.Humanoid.Health == 1 then
target = char
end
end
and it still didn’t work. Tried placing
script.Parent.StopAttackRemote.Event:Connect(function()
for i,v in pairs(game.Players:GetChildren()) do
if not game.Workspace:FindFirstChild(v.Name) then continue end local player = game.Workspace[v.Name] if player.Humanoid.Health <= 0 then hum.WalkSpeed = originalWalkSpeed canAttack = false end end
end)
on its attack script and still didn’t work. I’m pretty confused on how to fix it, and some videos on youtube haven’t helped either.
The scripts I made are below.
Main script:
local hum = script.Parent:WaitForChild(“Humanoid”)
local root = script.Parent:WaitForChild(“HumanoidRootPart”)local RunService = game:GetService(“RunService”)
local visionRange = 10
local attackRange = 3
local abandonRange = 20local target = nil
RunService.Heartbeat:Connect(function()
if target then
local plrRoot = target.HumanoidRootPart
local distance = (root.Position - plrRoot.Position).magnitudehum:MoveTo(plrRoot.Position - CFrame.new(root.Position, plrRoot.Position).LookVector * attackRange) if distance <= attackRange + 2 then script.AttackRemote:Fire(plrRoot) end if distance > abandonRange then target = nil end
else
for i,v in pairs(game.Players:GetChildren()) doif not game.Workspace:FindFirstChild(v.Name) then continue end local char = game.Workspace[v.Name] local plrRoot = char.HumanoidRootPart local distance = (root.Position - plrRoot.Position).magnitude if char.Humanoid.Health <= 0 then target = nil script.StopAttackRemote:Fire() else if char.Humanoid.Health > 1 or char.Humanoid.Health == 1 then target = char end end if distance < visionRange then target = char end end
end
end)
Attack script:
local attackCooldown = 1
local attackWindup = 1
local attackRange = 5
local attackDamage = 35local char = script.Parent.Parent
local hum = char:WaitForChild(“Humanoid”)
local originalWalkSpeed = hum.WalkSpeedlocal canAttack = true
script.Parent.AttackRemote.Event:Connect(function(plrRoot)
if not canAttack then return end
canAttack = falsehum.WalkSpeed = 0.1
local anim = Instance.new(“Animation”)
anim.AnimationId = “rbxassetid://6777953841”
local playAnim = hum:LoadAnimation(anim)
playAnim:Play()wait(attackWindup)
local newDistance = (char.HumanoidRootPart.Position - plrRoot.Position).magnitudeif newDistance <= attackRange + 5 then
plrRoot.Parent.Humanoid:TakeDamage(attackDamage)
end
wait(attackCooldown)
hum.WalkSpeed = originalWalkSpeed
canAttack = truescript.Parent.StopAttackRemote.Event:Connect(function()
for i,v in pairs(game.Players:GetChildren()) do if not game.Workspace:FindFirstChild(v.Name) then continue end local player = game.Workspace[v.Name] if player.Humanoid.Health <= 0 then hum.WalkSpeed = originalWalkSpeed canAttack = false end end
end)
end)
If anyone can help I’d be extremely grateful since Im a bit bad at scripting!
(Reposting since last one was barely barely barely BARELY seen)