AI attack script help

I have an AI attack script in my monster controller script. It works ok, but after the monster dies and breaks apart, he can still attack you while lying on the ground. I need to add a canattack restriction to this script. Can someone help me out.

I was thinking something like if the monsters health is <0 then canattack he false? Anyone have a solution to this?

local attackCooldown = 1.4
local attackWindup = 0.5
local attackRange = 3
local attackDamage = 15

local char = script.Parent.Parent
local hum = char:WaitForChild("Humanoid")
local originalWalkSpeed = hum.WalkSpeed

local canAttack = true

local sound = script.Parent.Parent.Tool.Handle.Hit

script.Parent.AttackRemote.Event:Connect(function(plrRoot)
	
	if not canAttack then return end
	canAttack = false
	
	--hum.WalkSpeed = 0.1
	
	local anim = Instance.new("Animation")
	anim.AnimationId = "rbxassetid://6760328690"
	local playAnim = hum:LoadAnimation(anim)
	playAnim:Play()
	sound:Play()
	
	wait(attackWindup)
	local newDistance = (char.HumanoidRootPart.Position - plrRoot.Position).magnitude
	
	if newDistance <= attackRange +5 then
		
		--if plrRoot.Parent.Player.isBlocking.Value == true then
				--attackDamage *= 0.5
		--end
		plrRoot.Parent.Humanoid:TakeDamage(attackDamage)
		
	end
	
	wait(attackCooldown)
	hum.WalkSpeed = originalWalkSpeed
	canAttack = true
	
end)

This might be something that could work

if not canAttack or health <=0 then return end
1 Like

You shou;d try what Viggx has said, and as a little side note you should replace all your wait() with task.wait()

1 Like

maybe remove the monster after its dead?

I tried this and it didnt work. I marked the lines I changed. This should work, not sure why it doesnt.

local attackCooldown = 1.4
local attackWindup = 0.5
local attackRange = 3
local attackDamage = 15

local char = script.Parent.Parent
local hum = char:WaitForChild("Humanoid")
local originalWalkSpeed = hum.WalkSpeed

local canAttack = true

local sound = script.Parent.Parent.Tool.Handle.swing

local health = script.Parent.Parent.Humanoid.Health <--------------------

script.Parent.AttackRemote.Event:Connect(function(plrRoot)
	
	if not canAttack or health <=0 then return end  <----------------------
	canAttack = false
	
	--hum.WalkSpeed = 0.1
	
	local anim = Instance.new("Animation")
	anim.AnimationId = "rbxassetid://6728995726"
	local playAnim = hum:LoadAnimation(anim)
	playAnim:Play()
	sound:Play()
	
	wait(attackWindup)
	local newDistance = (char.HumanoidRootPart.Position - plrRoot.Position).magnitude
	
	if newDistance <= attackRange +5 then
		
		--if plrRoot.Parent.Player.isBlocking.Value == true then
				--attackDamage *= 0.5
		--end
		plrRoot.Parent.Humanoid:TakeDamage(attackDamage)
		
	end
	
	wait(attackCooldown)
	hum.WalkSpeed = originalWalkSpeed
	canAttack = true
	
end)

Do you need the monster in the game after the health is 0 or could you just :Destroy() it?

like @2kvigilanxe said

I could destroy it, and that works, but It just looks strange when the monster simply vanishes from the game. It looks better if he crumbles to the ground and slowly vanishes after a few seconds.

I also tried this,


local attackCooldown = 1.4
local attackWindup = 0.5
local attackRange = 3
local attackDamage = 15

local char = script.Parent.Parent
local hum = char:WaitForChild("Humanoid")
local originalWalkSpeed = hum.WalkSpeed

local canAttack = true

local sound = script.Parent.Parent.Tool.Handle.swing

local health = script.Parent.Parent.Humanoid.Health

script.Parent.AttackRemote.Event:Connect(function(plrRoot)
	
	if not canAttack then
	canAttack = false
		
	end	
		
	if health <= 0 then
	canAttack = false
	end
	
	--hum.WalkSpeed = 0.1
	
	local anim = Instance.new("Animation")
	anim.AnimationId = "rbxassetid://6728995726"
	local playAnim = hum:LoadAnimation(anim)
	playAnim:Play()
	sound:Play()
	
	wait(attackWindup)
	local newDistance = (char.HumanoidRootPart.Position - plrRoot.Position).magnitude
	
	if newDistance <= attackRange +5 then
		
		--if plrRoot.Parent.Player.isBlocking.Value == true then
				--attackDamage *= 0.5
		--end
		plrRoot.Parent.Humanoid:TakeDamage(attackDamage)
		
	end
	
	wait(attackCooldown)
	hum.WalkSpeed = originalWalkSpeed
	canAttack = true
	
end)

I tried it this way also. I dont understand why this wont turn canAttack off.

local attackCooldown = 1.4
local attackWindup = 0.5
local attackRange = 3
local attackDamage = 15

local char = script.Parent.Parent
local hum = char:WaitForChild("Humanoid")
local originalWalkSpeed = hum.WalkSpeed

local canAttack = true

local sound = script.Parent.Parent.Tool.Handle.swing

local health = script.Parent.Parent.Humanoid.Health

script.Parent.AttackRemote.Event:Connect(function(plrRoot)
	
	if not canAttack then return end
	canAttack = false
		
	if health <= 0 then
		canAttack = false
	end
	
	--hum.WalkSpeed = 0.1
	
	local anim = Instance.new("Animation")
	anim.AnimationId = "rbxassetid://6728995726"
	local playAnim = hum:LoadAnimation(anim)
	playAnim:Play()
	sound:Play()
	
	wait(attackWindup)
	local newDistance = (char.HumanoidRootPart.Position - plrRoot.Position).magnitude
	
	if newDistance <= attackRange +5 then
		
		--if plrRoot.Parent.Player.isBlocking.Value == true then
				--attackDamage *= 0.5
		--end
		plrRoot.Parent.Humanoid:TakeDamage(attackDamage)
		
	end
	
	wait(attackCooldown)
	hum.WalkSpeed = originalWalkSpeed
	canAttack = true
	
end)

I think it doesnt work because turning off canattack just resets the script to the beginning.

Add a death animation if you don’t want it to just vanish

The reason it’s not working is you are saving the current health when you create the variable “health”:

local health = script.Parent.Parent.Humanoid.Health

Using this instead should work:

if not canAttack or hum.Health <=0 then return end
1 Like

Are you talking about this section?

while wait(1) do
	local targetIsInWorkspace
	pcall(function()
		targetIsInWorkspace = target.Parent == game.Workspace
	end)

Should I change it to ?

while task.wait() do
	local targetIsInWorkspace
	pcall(function()
		targetIsInWorkspace = target.Parent == game.Workspace
	end)

I had the 1 out of the script, but it searches for targets so fast I was worried it would slow down the server with too may monsters or people.

1 Like