Hello! I am trying to achieve a rush attack where the enemy charges at you and if it misses it will stop only a little behind the player but if it hits it will cause damage. What would be the best way to achieve this? My current setup looked something like this:
local function charge(distance, nearestPlayer, direction)
local oldWalk = ghost.Humanoid.WalkSpeed
if distance <= targetingDistance and distance >= stoppingDistance then
ghost.Humanoid.WalkSpeed = oldWalk + 30
wait(5)
ghost.Humanoid.WalkSpeed = oldWalk
end
end
local movementAnim = ghost.Humanoid.Animator:LoadAnimation(animations.movement)
movementAnim:Play()
--------------------------------------------------------
local attackList = {}
if config.attacks.slash.Value == true then
table.insert(attackList, slash)
end
if config.special_attacks.human_possession.Value == true then
table.insert(attackList, humanPossession)
end
if config.attacks.charge.Value == true then
table.insert(attackList, charge)
end
--------------------------------------------------------
local function locateTarget()
local playerList = game.Players:GetPlayers()
local distance = nil
local direction = nil
local nearestPlayer = nil
for _, player in pairs(playerList) do
local character = player.Character
if character then
local dvector = (player.Character.HumanoidRootPart.Position - ghost.HumanoidRootPart.Position)
if not nearestPlayer then
nearestPlayer = player
distance = dvector.Magnitude
direction = dvector.Unit
elseif dvector.Magnitude < distance then
nearestPlayer = player
distance = dvector.Magnitude
direction = dvector.Unit
end
end
end
return nearestPlayer, distance, direction
end
--------------------------------------------------------
while true and ghost.Humanoid.Health >= 1 do
local nearestPlayer, distance, direction = locateTarget()
if nearestPlayer then
if distance <= targetingDistance and distance >= stoppingDistance then
ghost.Humanoid:Move(direction)
else
ghost.Humanoid:Move(Vector3.new())
end
local pickedAttack = math.random(1, #attackList)
attackList[pickedAttack](distance, nearestPlayer, direction)
end
wait(0.1)
end
--------------------------------------------------------```
Thanks in advance for any help or advice that points me in the right direction!