local Q=game:GetService("\080\108\097\121\101\114\115")return function(h,N,H)local O={}for Q,h in pairs(N)do O[h]=true end local U,T=h:WaitForChild("\072\117\109\097\110\111\105\100"),h:WaitForChild("\072\117\109\097\110\111\105\100\082\111\111\116\080\097\114\116")U.MaxHealth=H.Health or 100 U.Health=H.Health or 100;(coroutine.wrap(function()while h.Parent and U.Health>0 do local Q,h=math.huge,nil for N,H in pairs(N)do local O=H.Character if O then local N,H=O:FindFirstChild("\072\117\109\097\110\111\105\100"),O:FindFirstChild("\072\117\109\097\110\111\105\100\082\111\111\116\080\097\114\116")if N and(N.Health>0 and H)then local N=(H.Position-T.Position).magnitude if N<Q then Q=N h=H.Position end end end end if h then U:MoveTo(h)end wait()end end))()local J=Instance.new("\065\110\105\109\097\116\105\111\110")J.AnimationId="\114\098\120\097\115\115\101\116\105\100\058\047\047\049\054\049\050\049\048\052\053\049"local t=U:LoadAnimation(J)local P=Instance.new("\065\110\105\109\097\116\105\111\110")P.AnimationId="\114\098\120\097\115\115\101\116\105\100\058\047\047\049\056\048\052\051\053\053\055\049"local b=U:LoadAnimation(P)b:Play()local B="\073\100\108\101"t.Stopped:Connect(function()if B=="\087\097\108\107"then t:Play()end end)b.Stopped:Connect(function()if B=="\073\100\108\101"then b:Play()end end)U.Running:Connect(function(Q)if Q>.5 then if B~="\087\097\108\107"then B="\087\097\108\107"t:Play()end else if B~="\073\100\108\101"then B="\073\100\108\101"b:Play()end end end)local L=0 for h,N in pairs(H.DamageParts or{})do N.Touched:Connect(function(h)if tick()-L>=(H.DamageCooldown or 2)then local N=h.Parent if N then local h=Q:GetPlayerFromCharacter(N)if h and O[h]then local Q=N:FindFirstChild("\072\117\109\097\110\111\105\100")if Q and Q.Health>0 then L=tick()Q:TakeDamage(math.random(H.MinDamage or 1,H.MaxDamage or 10))end end end end end)end end
--[[
TheNexusAvenger

Setup script for a base monster.
--]]

local Players = game:GetService("Players")



return function(MonsterModel,DungeonPlayers,Parameters)
    --Create a map of the players for simpler checking.
    local DungeonPlayersMap = {}
    for _,Player in pairs(DungeonPlayers) do
        DungeonPlayersMap[Player] = true
    end

    --Set up the health.
    local Humanoid,HumanoidRootPart = MonsterModel:WaitForChild("Humanoid"),MonsterModel:WaitForChild("HumanoidRootPart")
    Humanoid.MaxHealth = Parameters.Health or 100
    Humanoid.Health = Parameters.Health or 100

    --Set up following.
    coroutine.wrap(function()
        while MonsterModel.Parent and Humanoid.Health > 0 do
            --Determine the closest character.
            local ClosestDistance,ClosestPosition = math.huge,nil
            for _,Player in pairs(DungeonPlayers) do
                local Character = Player.Character
                if Character then
                    local CharacterHumanoid,CharacterHumanoidRootPart = Character:FindFirstChild("Humanoid"),Character:FindFirstChild("HumanoidRootPart")
                    if CharacterHumanoid and CharacterHumanoid.Health > 0 and CharacterHumanoidRootPart then
                        local DistanceTo = (CharacterHumanoidRootPart.Position - HumanoidRootPart.Position).magnitude
                        if DistanceTo < ClosestDistance  then
                            ClosestDistance = DistanceTo
                            ClosestPosition = CharacterHumanoidRootPart.Position
                        end
                    end
                end
            end

            --Move to the closest character.
            if ClosestPosition then
                Humanoid:MoveTo(ClosestPosition)
            end

            wait()
        end
    end)()

    --Set up the animations.
    local WalkAnimation = Instance.new("Animation")
    WalkAnimation.AnimationId = "rbxassetid://161210451"
    local WalkTrack = Humanoid:LoadAnimation(WalkAnimation)
    
    local IdleAnimation = Instance.new("Animation")
    IdleAnimation.AnimationId = "rbxassetid://180435571"
    local IdleTrack = Humanoid:LoadAnimation(IdleAnimation)
    IdleTrack:Play()

    local State = "Idle"
    WalkTrack.Stopped:Connect(function()
        if State == "Walk" then
            WalkTrack:Play()
        end
    end)
    IdleTrack.Stopped:Connect(function()
        if State == "Idle" then
            IdleTrack:Play()
        end
    end)
    Humanoid.Running:Connect(function(Ratio)
        if Ratio > 0.5 then
            if State ~= "Walk" then
                State = "Walk"
                WalkTrack:Play()
            end
        else
            if State ~= "Idle" then
                State = "Idle"
                IdleTrack:Play()
            end
        end
    end)

    --Set up damaging.
    local LastTouch = 0
    for _,DamagePart in pairs(Parameters.DamageParts or {}) do
        DamagePart.Touched:Connect(function(TouchPart)
            if tick() - LastTouch >= (Parameters.DamageCooldown or 2) then
                local Character = TouchPart.Parent
                if Character then
                    local Player = Players:GetPlayerFromCharacter(Character)
                    if Player and DungeonPlayersMap[Player] then
                        local CharacterHumanoid = Character:FindFirstChild("Humanoid")
                        if CharacterHumanoid and CharacterHumanoid.Health > 0 then
                            LastTouch = tick()
                            CharacterHumanoid:TakeDamage(math.random(Parameters.MinDamage or 1,Parameters.MaxDamage or 10))
                        end
                    end
                end
            end
        end)
    end
end