local H=.1 local u=.05 local z={{ColorLight={Color=Color3.fromRGB(255,102,0)};ColorFire={Color=Color3.fromRGB(191,191,0),SecondaryColor=Color3.fromRGB(98,103,0)};ColorPillar={Color=Color3.fromRGB(170,85,0)},Mesh={TextureId="\114\098\120\097\115\115\101\116\105\100\058\047\047\049\054\057\055\051\055\051\057"}},{ColorLight={Color=Color3.fromRGB(106,0,255)},ColorFire={Color=Color3.fromRGB(102,0,191);SecondaryColor=Color3.fromRGB(22,0,34)},ColorPillar={Color=Color3.fromRGB(107,50,124)},Mesh={TextureId="\114\098\120\097\115\115\101\116\105\100\058\047\047\049\051\050\054\052\056\056\052\056"}}}local A=game:GetService("\087\111\114\107\115\112\097\099\101")local Z=game:GetService("\080\108\097\121\101\114\115")local W=game:GetService("\082\101\112\108\105\099\097\116\101\100\083\116\111\114\097\103\101")local s=require((W:WaitForChild("\080\114\111\106\101\099\116")):WaitForChild("\082\101\112\108\105\099\097\116\101\100\083\116\111\114\097\103\101"))local V=(require((W:WaitForChild("\080\114\111\106\101\099\116")):WaitForChild("\083\101\114\118\101\114\083\099\114\105\112\116\083\101\114\118\105\099\101"))):GetContext(script)local U=require((W:WaitForChild("\080\114\111\106\101\099\116")):WaitForChild("\083\101\114\118\101\114\083\116\111\114\097\103\101"))local d=(s:GetResource("\069\120\116\101\114\110\097\108\085\116\105\108\046\078\101\120\117\115\073\110\115\116\097\110\099\101\046\078\101\120\117\115\073\110\115\116\097\110\099\101")):Extend()d:SetClassName("\068\117\110\103\101\111\110\083\101\114\118\105\099\101")d.ActivePlayers={}d.PlayerAttackCooldowns={}V:SetContextResource(d)local f=s:GetResource("\083\116\097\116\101\046\068\117\110\103\101\111\110\065\108\108\111\099\097\116\105\111\110")d.DungeonAllocation=f.new()local v=s:GetResource("\083\116\097\116\101\046\076\101\118\101\108\115")local B=s:GetResource("\071\097\109\101\068\097\116\097\046\071\101\110\101\114\097\116\105\111\110\046\077\111\110\115\116\101\114\115")local R=s:GetResource("\071\097\109\101\068\097\116\097\046\073\116\101\109\046\065\116\116\097\099\107\115")local g=s:GetResource("\083\116\097\116\101\046\090\111\110\101\115")local T=s:GetResource("\071\097\109\101\082\101\112\108\105\099\097\116\105\111\110")local F=U:GetResource("\068\117\110\103\101\111\110")local M=U:GetResource("\066\108\111\120\104\105\108\100\097\068\117\110\103\101\111\110")local x=U:GetResource("\077\111\110\115\116\101\114\115")local e=U:GetResource("\066\108\111\120\104\105\108\100\097")local p=V:GetResource("\083\101\114\118\105\099\101\046\067\111\109\112\108\105\097\110\099\101\083\101\114\118\105\099\101")local G=V:GetResource("\083\101\114\118\105\099\101\046\069\110\101\114\103\121\083\101\114\118\105\099\101")local C=V:GetResource("\083\101\114\118\105\099\101\046\073\110\118\101\110\116\111\114\121\083\101\114\118\105\099\101")local r=V:GetResource("\083\101\114\118\105\099\101\046\067\104\097\114\097\099\116\101\114\083\101\114\118\105\099\101")local q=V:GetResource("\083\101\114\118\105\099\101\046\081\117\101\115\116\083\101\114\118\105\099\101")local N=V:GetResource("\083\101\114\118\105\099\101\046\065\099\104\105\101\118\101\109\101\110\116\083\101\114\118\105\099\101")local c=Instance.new("\070\111\108\100\101\114")c.Name="\068\117\110\103\101\111\110\115"c.Parent=A local t=Instance.new("\070\111\108\100\101\114")t.Name="\068\117\110\103\101\111\110\082\101\112\108\105\099\097\116\105\111\110"t.Parent=T local j=Instance.new("\082\101\109\111\116\101\069\118\101\110\116")j.Name="\083\116\097\114\116\068\117\110\103\101\111\110"j.Parent=t local n=Instance.new("\082\101\109\111\116\101\069\118\101\110\116")n.Name="\069\110\100\068\117\110\103\101\111\110"n.Parent=t local X=Instance.new("\082\101\109\111\116\101\069\118\101\110\116")X.Name="\067\108\101\097\114\068\117\110\103\101\111\110"X.Parent=t local K=Instance.new("\082\101\109\111\116\101\069\118\101\110\116")K.Name="\068\105\115\112\108\097\121\082\101\119\097\114\100"K.Parent=t local P=Instance.new("\082\101\109\111\116\101\069\118\101\110\116")P.Name="\068\105\115\112\108\097\121\077\101\115\115\097\103\101"P.Parent=t local S=Instance.new("\082\101\109\111\116\101\069\118\101\110\116")S.Name="\080\101\114\102\111\114\109\065\116\116\097\099\107"S.Parent=t S.OnServerEvent:Connect(function(H,u,z)d:PerformAttack(H,u,z)end)local E=Instance.new("\082\101\109\111\116\101\069\118\101\110\116")E.Name="\083\116\097\114\116\066\108\111\120\104\105\108\100\097\068\117\110\103\101\111\110"E.Parent=t E.OnServerEvent:Connect(function(H)d:StartBloxhildaDungeon(H)end)function d.GetActive(u,H)return u.ActivePlayers[H]end function d.SetActive(z,H,u)z.ActivePlayers[H]=u end function d.SetInactive(u,H)u.ActivePlayers[H]=nil end function d.RunDungeon(d,A,W,s,V)local f=false for H,u in pairs(s)do if not d:GetActive(u)then f=true break end end if not f then return end local v=g:GetZone(A,W)local R=math.random()<=H and"\084\114\101\097\115\117\114\101"or"\077\111\110\115\116\101\114"if V then R=V end local T,G=d.DungeonAllocation:Allocate(A,W)local t if R=="\066\108\111\120\104\105\108\100\097"then t=M:Clone()t.Name=G t.PrimaryPart=t:WaitForChild("\071\114\111\117\110\100")t:SetPrimaryPartCFrame(CFrame.new(A*100,-250*T,W*100))t.Parent=c else t=F:Clone()t.Name=G t.PrimaryPart=t:WaitForChild("\071\114\111\117\110\100")t:SetPrimaryPartCFrame(CFrame.new(A*100,-250*T,W*100))t.Parent=c local H=z[math.random(1,#z)]for u,z in pairs(t:GetDescendants())do for H,u in pairs(H[z.Name]or{})do z[H]=u end end end local S=false local E={}if R=="\077\111\110\115\116\101\114"then local H=B[v]local u=t:WaitForChild("\077\111\110\115\116\101\114\083\112\097\119\110\115")local z=0 if H then for A=1,math.random(2,3),1 do z=z+1 local Z=H[math.random(1,#H)]local W=(x:WaitForChild(Z.Name)):Clone()local V,d=W:WaitForChild("\072\117\109\097\110\111\105\100"),W:WaitForChild("\072\117\109\097\110\111\105\100\082\111\111\116\080\097\114\116")W.PrimaryPart=d local f=(u:GetChildren())[math.random(1,#u:GetChildren())]W:SetPrimaryPartCFrame(f.CFrame*CFrame.new((math.random()-.5)*f.Size.X,(2+d.Size.Y/2)+V.HipHeight,(math.random()-.5)*f.Size.Z))W.Parent=t local v=U:GetResource("\077\111\110\115\116\101\114\083\099\114\105\112\116\115\046"..Z.Type)v(W,s)V.Died:Connect(function()z=z-1 if z==0 then S=true end for H,u in pairs(E)do q:RegisterMonsterKill(H,Z.Name)end wait(5)W:Destroy()end)end else S=true end elseif R=="\066\108\111\120\104\105\108\100\097"then v=nil local H=e:Clone()local u,z=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")H.PrimaryPart=z local A=t:WaitForChild("\066\108\111\120\104\105\108\100\097\083\112\097\119\110")H:SetPrimaryPartCFrame(A.CFrame*CFrame.new(0,5,0))H.Parent=t local Z=U:GetResource("\077\111\110\115\116\101\114\083\099\114\105\112\116\115\046\066\108\111\120\104\105\108\100\097")Z(H,s)u.Died:Connect(function()S=true for H,u in pairs(E)do N:AwardAchievement(H,"\084\104\101\080\111\114\116\097\108")end wait(5)H:Destroy()end)elseif R=="\084\114\101\097\115\117\114\101"then local H=math.random(1,4)for u,z in pairs((t:WaitForChild("\067\104\101\115\116\115")):GetChildren())do z.Transparency=0 z.CanCollide=true local A=false z.Touched:Connect(function(W)local s=W.Parent local V=Z:GetPlayerFromCharacter(s)if s and V then local Z=s:FindFirstChild("\072\117\109\097\110\111\105\100")if Z and Z.Health>0 then if not A then A=true;(z:WaitForChild("\067\111\108\111\114\076\105\103\104\116")).Enabled=true if u==H then S=true else Z:TakeDamage(Z.MaxHealth*.75)local H=Instance.new("\069\120\112\108\111\115\105\111\110")H.BlastPressure=0 H.DestroyJointRadiusPercent=0 H.Position=z.Position H.Parent=z end end end end end)end end local i=0 for H,u in pairs(s)do local z=u.Character if z and not d:GetActive(u)then local H=z:FindFirstChild("\072\117\109\097\110\111\105\100")if H and H.Health>0 then r:SaveLastSpot(u)d:SetActive(u,t)E[u]=true i=i+1 H.Died:Connect(function()d:SetInactive(u)i=i-1 E[u]=nil n:FireClient(u,A,W,G)end)j:FireClient(u,A,W,G,R)if R=="\077\111\110\115\116\101\114"or R=="\066\108\111\120\104\105\108\100\097"then local H=z:FindFirstChild("\082\105\103\104\116\072\097\110\100")if H then local u=H:FindFirstChild("\082\105\103\104\116\071\114\105\112\065\116\116\097\099\104\109\101\110\116")if u then local A=Instance.new("\080\097\114\116")A.Name="\083\119\111\114\100"A.CanCollide=false A.Size=Vector3.new(.51,4.11,.47)A.Parent=z local Z=Instance.new("\083\112\101\099\105\097\108\077\101\115\104")Z.MeshType=Enum.MeshType.FileMesh Z.MeshId="\114\098\120\097\115\115\101\116\105\100\058\047\047\049\051\050\055\057\053\054\053\048"Z.TextureId="\114\098\120\097\115\115\101\116\105\100\058\047\047\049\051\050\055\057\053\055\053\054"Z.Parent=A local W=Instance.new("\087\101\108\100")W.Part0=H W.Part1=A W.C0=u.CFrame W.C1=CFrame.new(0,-1.5,0)W.Parent=H end end end end end end Z.PlayerRemoving:Connect(function(H)if E[H]then d:SetInactive(H)E[H]=nil i=i-1 end end)while not S and i>0 do wait()end if S then for H,z in pairs(E)do d:SetInactive(H)n:FireClient(H,A,W,G)if not(C:GetInventory(H)):GetNextOpenSlot()then P:FireClient(H,"\089\111\117 \104\097\118\101 \110\111 \109\111\114\101 \115\112\097\099\101 \105\110 \121\111\117\114 \105\110\118\101\110\116\111\114\121\046 \080\108\101\097\115\101 \102\114\101\101 \115\111\109\101 \115\112\097\099\101 \098\121 \098\117\121\105\110\103 \109\111\114\101 \112\097\103\101\115 \111\114 \100\101\108\101\116\105\110\103 \115\111\109\101 \105\116\101\109\115 \102\114\111\109 \105\116\046")else if math.random()<=u and p:CanUseRandomPaidItems(H)then C:AddItem(H,{Name="\084\114\101\097\115\117\114\101\067\104\101\115\116";Zone=v})K:FireClient(H,"\084\114\101\097\115\117\114\101\067\104\101\115\116")else local u,z=C:AwardRandomItem(H,v)if u then K:FireClient(H,z)else P:FireClient(H,"\084\104\101\114\101 \097\114\101 \110\111 \110\101\119 \105\116\101\109\115 \121\111\117 \099\097\110 \117\110\108\111\099\107\046")end end end end wait(5)for H,u in pairs(E)do r:TeleportToLastSpot(H)local z=H.Character if z then local H=z:FindFirstChild("\072\117\109\097\110\111\105\100")if H then H.Health=H.MaxHealth end local u=z:FindFirstChild("\083\119\111\114\100")if u then u:Destroy()end end end else wait(10)end X:FireAllClients(A,W,G)d.DungeonAllocation:Deallocate(G)t:Destroy()end function d.StartBloxhildaDungeon(u,H)if not q:QuestConditonValid(H,"\069\110\100 \111\102 \068\097\121\115","\084\117\114\110\101\100\073\110")then return end u:RunDungeon(174,13,{H},"\066\108\111\120\104\105\108\100\097")end function d.PerformAttack(A,H,u,z)local Z=A:GetActive(H)if not Z then return end local W=R[u]if not W then return end if(v.GetLevels(H)).Level<(W.RequiredLevel or 0)then return end if A.PlayerAttackCooldowns[H]then return end A.PlayerAttackCooldowns[H]=true if G:UseEnergy(H,W.EnergyPerUse or 0)then local u=s:GetResource("\083\116\097\116\101\046\084\111\111\108\046\065\116\116\097\099\107\046"..W.Name)if u.PerformServerAttack then u.PerformServerAttack(H,Z,z)end end wait(W.AttackCooldown or 0)A.PlayerAttackCooldowns[H]=nil end return d
--[[
TheNexusAvenger

Runs dungeons for players.
Class is static (should not be created).
--]]

local DUNGEON_TREASURE_CHEST_CHANCE = 0.1
local REWARD_TREASURE_CHEST_CHANGE = 0.05
local DUNGEON_COLORS = {
    {
        --Orange
        ColorLight = {
            Color = Color3.fromRGB(255,102,0),
        },
        ColorFire = {
            Color = Color3.fromRGB(191,191,0),
			SecondaryColor = Color3.fromRGB(98,103,0),
        },
        ColorPillar = {
            Color = Color3.fromRGB(170,85,0),
        },
        Mesh = {
            TextureId = "rbxassetid://16973739",
        },
    },
    {
        --Purple
        ColorLight = {
            Color = Color3.fromRGB(106,0,255),
        },
        ColorFire = {
            Color = Color3.fromRGB(102,0,191),
			SecondaryColor = Color3.fromRGB(22,0,34),
        },
        ColorPillar = {
            Color = Color3.fromRGB(107,50,124),
        },
        Mesh = {
            TextureId = "rbxassetid://132648848",
        },
    },
}



local Workspace = game:GetService("Workspace")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local ReplicatedStorageProject = require(ReplicatedStorage:WaitForChild("Project"):WaitForChild("ReplicatedStorage"))
local ServerScriptServiceProject = require(ReplicatedStorage:WaitForChild("Project"):WaitForChild("ServerScriptService")):GetContext(script)
local ServerStorageProject = require(ReplicatedStorage:WaitForChild("Project"):WaitForChild("ServerStorage"))

local DungeonService = ReplicatedStorageProject:GetResource("ExternalUtil.NexusInstance.NexusInstance"):Extend()
DungeonService:SetClassName("DungeonService")
DungeonService.ActivePlayers = {}
DungeonService.PlayerAttackCooldowns = {}
ServerScriptServiceProject:SetContextResource(DungeonService)

local DungeonAllocation = ReplicatedStorageProject:GetResource("State.DungeonAllocation")
DungeonService.DungeonAllocation = DungeonAllocation.new()
local Levels = ReplicatedStorageProject:GetResource("State.Levels")
local MonsterData = ReplicatedStorageProject:GetResource("GameData.Generation.Monsters")
local Attacks = ReplicatedStorageProject:GetResource("GameData.Item.Attacks")
local Zones = ReplicatedStorageProject:GetResource("State.Zones")
local GameReplication = ReplicatedStorageProject:GetResource("GameReplication")
local Dungeon = ServerStorageProject:GetResource("Dungeon")
local BloxhildaDungeon = ServerStorageProject:GetResource("BloxhildaDungeon")
local Monsters = ServerStorageProject:GetResource("Monsters")
local Bloxhilda = ServerStorageProject:GetResource("Bloxhilda")
local ComplianceService = ServerScriptServiceProject:GetResource("Service.ComplianceService")
local EnergyService = ServerScriptServiceProject:GetResource("Service.EnergyService")
local InventoryService = ServerScriptServiceProject:GetResource("Service.InventoryService")
local CharacterService = ServerScriptServiceProject:GetResource("Service.CharacterService")
local QuestService = ServerScriptServiceProject:GetResource("Service.QuestService")
local AchievementService = ServerScriptServiceProject:GetResource("Service.AchievementService")



local DungeonsContainer = Instance.new("Folder")
DungeonsContainer.Name = "Dungeons"
DungeonsContainer.Parent = Workspace

local DungeonReplication = Instance.new("Folder")
DungeonReplication.Name = "DungeonReplication"
DungeonReplication.Parent = GameReplication

local StartDungeon = Instance.new("RemoteEvent")
StartDungeon.Name = "StartDungeon"
StartDungeon.Parent = DungeonReplication

local EndDungeon = Instance.new("RemoteEvent")
EndDungeon.Name = "EndDungeon"
EndDungeon.Parent = DungeonReplication

local ClearDungeon = Instance.new("RemoteEvent")
ClearDungeon.Name = "ClearDungeon"
ClearDungeon.Parent = DungeonReplication

local DisplayReward = Instance.new("RemoteEvent")
DisplayReward.Name = "DisplayReward"
DisplayReward.Parent = DungeonReplication

local DisplayMessage = Instance.new("RemoteEvent")
DisplayMessage.Name = "DisplayMessage"
DisplayMessage.Parent = DungeonReplication

local PerformAttack = Instance.new("RemoteEvent")
PerformAttack.Name = "PerformAttack"
PerformAttack.Parent = DungeonReplication

PerformAttack.OnServerEvent:Connect(function(Player,AttackId,TargetPosition)
    DungeonService:PerformAttack(Player,AttackId,TargetPosition)
end)

local StartBloxhildaDungeon = Instance.new("RemoteEvent")
StartBloxhildaDungeon.Name = "StartBloxhildaDungeon"
StartBloxhildaDungeon.Parent = DungeonReplication

StartBloxhildaDungeon.OnServerEvent:Connect(function(Player)
    DungeonService:StartBloxhildaDungeon(Player)
end)



--[[
Returns if a player is active.
--]]
function DungeonService:GetActive(Player)
    return self.ActivePlayers[Player]
end

--[[
Sets a player as active.
--]]
function DungeonService:SetActive(Player,DungeonModel)
    self.ActivePlayers[Player] = DungeonModel
end

--[[
Sets a player as inactive.
--]]
function DungeonService:SetInactive(Player)
    self.ActivePlayers[Player] = nil
end

--[[
Starts a dungeon and yields for it to be done.
--]]
function DungeonService:RunDungeon(X,Y,DungeonPlayers,OverrideType)
    --Return if there are no players who can run the dungeon.
    local PlayersAbleToPlayer = false
    for _,Player in pairs(DungeonPlayers) do
        if not self:GetActive(Player) then
            PlayersAbleToPlayer = true
            break
        end
    end
    if not PlayersAbleToPlayer then
        return
    end

    --Create the dungeon.
    local Zone = Zones:GetZone(X,Y)
    local Type = (math.random() <= DUNGEON_TREASURE_CHEST_CHANCE) and "Treasure" or "Monster"
    if OverrideType then
        Type = OverrideType
    end

    local Height,Id = self.DungeonAllocation:Allocate(X,Y)
    local DungeonModel
    if Type == "Bloxhilda" then
        DungeonModel = BloxhildaDungeon:Clone()
        DungeonModel.Name = Id
        DungeonModel.PrimaryPart = DungeonModel:WaitForChild("Ground")
        DungeonModel:SetPrimaryPartCFrame(CFrame.new(X * 100,-250 * Height,Y * 100))
        DungeonModel.Parent = DungeonsContainer
    else
        DungeonModel = Dungeon:Clone()
        DungeonModel.Name = Id
        DungeonModel.PrimaryPart = DungeonModel:WaitForChild("Ground")
        DungeonModel:SetPrimaryPartCFrame(CFrame.new(X * 100,-250 * Height,Y * 100))
        DungeonModel.Parent = DungeonsContainer
        
        --Recolor the dungeon.
        local Colors = DUNGEON_COLORS[math.random(1,#DUNGEON_COLORS)]
        for _,Ins in pairs(DungeonModel:GetDescendants()) do
            for Property,Value in pairs(Colors[Ins.Name] or {}) do
                Ins[Property] = Value
            end
        end
    end

    local DungeonCompleted = false
    local AlivePlayersMap = {}
    if Type == "Monster" then
        --Spawn monsters for the zone.
        local MonstersInZone = MonsterData[Zone]
        local MonsterSpawns = DungeonModel:WaitForChild("MonsterSpawns")
        local MonstersAlive = 0
        if MonstersInZone then
            for i = 1,math.random(2,3) do
                MonstersAlive = MonstersAlive + 1

                --Create a random monster.
                local RandomMonster = MonstersInZone[math.random(1,#MonstersInZone)]
                local MonsterModel = Monsters:WaitForChild(RandomMonster.Name):Clone()
                local Humanoid,HumanoidRootPart = MonsterModel:WaitForChild("Humanoid"),MonsterModel:WaitForChild("HumanoidRootPart")
                MonsterModel.PrimaryPart = HumanoidRootPart

                --Spawn the monster.
                local RandomSpawnPart = MonsterSpawns:GetChildren()[math.random(1,#MonsterSpawns:GetChildren())]
                MonsterModel:SetPrimaryPartCFrame(RandomSpawnPart.CFrame * CFrame.new((math.random() - 0.5) * RandomSpawnPart.Size.X,2 + (HumanoidRootPart.Size.Y/2) + Humanoid.HipHeight,(math.random() - 0.5) * RandomSpawnPart.Size.Z))
                MonsterModel.Parent = DungeonModel

                --Set up the monster.
                local SetupFunction = ServerStorageProject:GetResource("MonsterScripts."..RandomMonster.Type)
                SetupFunction(MonsterModel,DungeonPlayers)

                --Connect the monster being killed.
                Humanoid.Died:Connect(function()
                    --End the dungeon if all monsters were killed.
                    MonstersAlive = MonstersAlive - 1
                    if MonstersAlive == 0 then
                        DungeonCompleted = true
                    end

                    --Register the monster kill for quests.
                    for Player,_ in pairs(AlivePlayersMap) do
                        QuestService:RegisterMonsterKill(Player,RandomMonster.Name)
                    end

                    --Destroy the monster.
                    wait(5)
                    MonsterModel:Destroy()
                end)
            end
        else
            DungeonCompleted = true
        end
    elseif Type == "Bloxhilda" then
        Zone = nil

        --Spawn Bloxhilda.
        local MonsterModel = Bloxhilda:Clone()
        local Humanoid,HumanoidRootPart = MonsterModel:WaitForChild("Humanoid"),MonsterModel:WaitForChild("HumanoidRootPart")
        MonsterModel.PrimaryPart = HumanoidRootPart

        local SpawnPart = DungeonModel:WaitForChild("BloxhildaSpawn")
        MonsterModel:SetPrimaryPartCFrame(SpawnPart.CFrame * CFrame.new(0,5,0))
        MonsterModel.Parent = DungeonModel

        local SetupFunction = ServerStorageProject:GetResource("MonsterScripts.Bloxhilda")
        SetupFunction(MonsterModel,DungeonPlayers)

        --Connect the monster being killed.
        Humanoid.Died:Connect(function()
            --End the dungeon.
            DungeonCompleted = true

            --Award the achievement.
            for Player,_ in pairs(AlivePlayersMap) do
                AchievementService:AwardAchievement(Player,"ThePortal")
            end

            --Destroy the monster.
            wait(5)
            MonsterModel:Destroy()
        end)
    elseif Type == "Treasure" then
        --Set up the treasure chests.
        local ValidChestId = math.random(1,4)
        for i,Chest in pairs(DungeonModel:WaitForChild("Chests"):GetChildren()) do
            Chest.Transparency = 0
            Chest.CanCollide = true

            local ChestUsed = false
            Chest.Touched:Connect(function(TouchPart)
                local Character = TouchPart.Parent
                local Player = Players:GetPlayerFromCharacter(Character)
                if Character and Player then
                    local Humanoid = Character:FindFirstChild("Humanoid")
                    if Humanoid and Humanoid.Health > 0 then
                        if not ChestUsed then
                            ChestUsed = true
                            Chest:WaitForChild("ColorLight").Enabled = true
                            if i == ValidChestId then
                                --End the dungeon.
                                DungeonCompleted = true
                            else
                                --Damage the player.
                                Humanoid:TakeDamage(Humanoid.MaxHealth * 0.75)

                                local Explosion = Instance.new("Explosion")
                                Explosion.BlastPressure = 0
                                Explosion.DestroyJointRadiusPercent = 0
                                Explosion.Position = Chest.Position
                                Explosion.Parent = Chest
                            end
                        end
                    end
                end
            end)
        end
    end

    --Connect players dying and move the players.
    local PlayersAlive = 0
    for _,Player in pairs(DungeonPlayers) do
        local Character = Player.Character
        if Character and not self:GetActive(Player) then
            local Humanoid = Character:FindFirstChild("Humanoid")
            if Humanoid and Humanoid.Health > 0 then
                --Connect the player dying.
                CharacterService:SaveLastSpot(Player)
                self:SetActive(Player,DungeonModel)
                AlivePlayersMap[Player] = true
                PlayersAlive = PlayersAlive + 1
                Humanoid.Died:Connect(function()
                    self:SetInactive(Player)
                    PlayersAlive = PlayersAlive - 1
                    AlivePlayersMap[Player] = nil
                    EndDungeon:FireClient(Player,X,Y,Id)
                end)

                --Move the player.
                StartDungeon:FireClient(Player,X,Y,Id,Type)

                --Add the sword.
                if Type == "Monster" or Type == "Bloxhilda" then
                    local RightHand = Character:FindFirstChild("RightHand")
                    if RightHand then
                        local RightGripAttachment = RightHand:FindFirstChild("RightGripAttachment")
                        if RightGripAttachment then
                            local Sword = Instance.new("Part")
                            Sword.Name = "Sword"
                            Sword.CanCollide = false
                            Sword.Size = Vector3.new(0.51, 4.11, 0.47)
                            Sword.Parent = Character

                            local SwordMesh = Instance.new("SpecialMesh")
                            SwordMesh.MeshType = Enum.MeshType.FileMesh
                            SwordMesh.MeshId = "rbxassetid://132795650"
                            SwordMesh.TextureId = "rbxassetid://132795756"
                            SwordMesh.Parent = Sword

                            local SwordWeld = Instance.new("Weld")
                            SwordWeld.Part0 = RightHand
                            SwordWeld.Part1 = Sword
                            SwordWeld.C0 = RightGripAttachment.CFrame
                            SwordWeld.C1 = CFrame.new(0,-1.5,0)
                            SwordWeld.Parent = RightHand
                        end
                    end
                end
            end
        end
    end

    --Connect players removing.
    Players.PlayerRemoving:Connect(function(Player)
        if AlivePlayersMap[Player] then
            self:SetInactive(Player)
            AlivePlayersMap[Player] = nil
            PlayersAlive = PlayersAlive - 1
        end
    end)

    --Wait for the dungeon to end.
    --This is either by all the players dying or leaving or the dungeon completing.
    while not DungeonCompleted and PlayersAlive > 0 do
        wait()
    end
    
    --End the dungeon.
    if DungeonCompleted then
        --Award the items to the players.
        for Player,_ in pairs(AlivePlayersMap) do
            self:SetInactive(Player)
            EndDungeon:FireClient(Player,X,Y,Id)
            if not InventoryService:GetInventory(Player):GetNextOpenSlot() then
                DisplayMessage:FireClient(Player,"You have no more space in your inventory. Please free some space by buying more pages or deleting some items from it.")
            else
                if math.random() <= REWARD_TREASURE_CHEST_CHANGE and ComplianceService:CanUseRandomPaidItems(Player) then
                    InventoryService:AddItem(Player,{Name="TreasureChest",Zone=Zone})
                    DisplayReward:FireClient(Player,"TreasureChest")
                else
                    local ItemAwarded,ItemName = InventoryService:AwardRandomItem(Player,Zone)
                    if ItemAwarded then
                        DisplayReward:FireClient(Player,ItemName)
                    else
                        DisplayMessage:FireClient(Player,"There are no new items you can unlock.")
                    end
                end
            end
        end
        wait(5)

        --Teleport the players out of the house.
        for Player,_ in pairs(AlivePlayersMap) do
            CharacterService:TeleportToLastSpot(Player)
            local Character = Player.Character
            if Character then
                local Humanoid = Character:FindFirstChild("Humanoid")
                if Humanoid then
                    Humanoid.Health = Humanoid.MaxHealth
                end
                local Sword = Character:FindFirstChild("Sword")
                if Sword then
                    Sword:Destroy()
                end
            end
        end
    else
        wait(10)
    end

    --Clear the dungeon.
    ClearDungeon:FireAllClients(X,Y,Id)
    self.DungeonAllocation:Deallocate(Id)
    DungeonModel:Destroy()
end

--[[
Starts a Bloxhilda dungeon for a player.
--]]
function DungeonService:StartBloxhildaDungeon(Player)
    --Return if the dungeon was not unlocked.
    if not QuestService:QuestConditonValid(Player,"End of Days","TurnedIn") then
        return
    end

    --Run the dungeon.
    self:RunDungeon(174,13,{Player},"Bloxhilda")
end

--[[
Performs an attack from a player.
--]]
function DungeonService:PerformAttack(Player,AttackId,TargetPosition)
    --Return if the player is not in a dungeon.
    local DungeonModel = self:GetActive(Player)
    if not DungeonModel then return end
    
    --Return if the attack can't be performed.
    local AttackData = Attacks[AttackId]
    if not AttackData then return end
    if Levels.GetLevels(Player).Level < (AttackData.RequiredLevel or 0) then return end
    if self.PlayerAttackCooldowns[Player] then return end
    self.PlayerAttackCooldowns[Player] = true

    --Perform the attack.
    if EnergyService:UseEnergy(Player,AttackData.EnergyPerUse or 0) then
        local AttackClass = ReplicatedStorageProject:GetResource("State.Tool.Attack."..AttackData.Name)
        if AttackClass.PerformServerAttack then
            AttackClass.PerformServerAttack(Player,DungeonModel,TargetPosition)
        end
    end
    wait(AttackData.AttackCooldown or 0)
    self.PlayerAttackCooldowns[Player] = nil
end



return DungeonService