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)
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)
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)