#I need a script that will make the monster after detecting player, charge at him

9 messages · Page 1 of 1 (latest)

zenith stream
#

right now the script looks like this

#

local larm = script.Parent:FindFirstChild("HumanoidRootPart")
local rarm = script.Parent:FindFirstChild("HumanoidRootPart")

function findNearestTorso(pos)
    local list = game.Workspace:children()
    local torso = nil
    local dist = math.huge
    local temp = nil
    local human = nil
    local temp2 = nil
    for x = 1, #list do
        temp2 = list[x]
        if (temp2.className == "Model") and (temp2 ~= script.Parent) then
            temp = temp2:findFirstChild("HumanoidRootPart")
            human = temp2:findFirstChild("Humanoid")
            if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
                if (temp.Position - pos).magnitude < dist then
                    torso = temp
                    dist = (temp.Position - pos).magnitude
                end
            end
        end
    end
    return torso
end




while true do
    wait(0)
    local target = findNearestTorso(script.Parent.HumanoidRootPart.Position)
    if target ~= nil then
        script.Parent.Humanoid:MoveTo(target.Position, target)
    end

end
cold locust
#

if you want to run at the player each tick, you can use RenderStepped from RunService

#

so first you will have to find a target

local function FindTarget()
  ...
end

[RenderSteepEvent]
  local target = FindTarget()
end
#

then, check if there is no other active target

#
local activeTarget

[RenderSteepEvent]
  if activeTarget then return end
  local target = FindTarget()
  if not target then return end
  activeTarget = target
end
#

like that, if you find a taget, the monster wont charge to multiple target

#

then you can create functions / class with events for your monster.s if you have 1 or multiple

[RenderSteepEvent]
  if monster.isAttacking then return end
  local target = monster:FindTarget()
  if not target then return end
  monster.isAttacking = true
  monster:Charge(target) -- Runs at player
  monster:Reload(target) -- When he move 15 studs back
  monster.isAttacking = false -- To make it search another target
end