local a=game:GetService("\087\111\114\107\115\112\097\099\101")local d=game:GetService("\080\108\097\121\101\114\115")local T=game:GetService("\082\101\112\108\105\099\097\116\101\100\083\116\111\114\097\103\101")local j=require((T: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 D=j:GetResource("\071\097\109\101\082\101\112\108\105\099\097\116\105\111\110\046\068\117\110\103\101\111\110\082\101\112\108\105\099\097\116\105\111\110\046\083\116\097\114\116\068\117\110\103\101\111\110")local p=j:GetResource("\071\097\109\101\082\101\112\108\105\099\097\116\105\111\110\046\068\117\110\103\101\111\110\082\101\112\108\105\099\097\116\105\111\110\046\067\108\101\097\114\068\117\110\103\101\111\110")local X=a:WaitForChild("\068\117\110\103\101\111\110\115")local G={}local B local function g()local a=G[B]if a then local T=d.LocalPlayer.Character if T then local d=T:FindFirstChild("\072\117\109\097\110\111\105\100\082\111\111\116\080\097\114\116")if d then local T=a:WaitForChild("\083\112\097\119\110")local j=(CFrame.new(d.Position)):inverse()*d.CFrame d.CFrame=(T.CFrame*CFrame.new(math.random()*(T.Size.X/2),0,math.random()*(T.Size.Z/2)))*j end end end end local function m(a)G[a.Name]=a if a.Name==B then g()end end for a,d in pairs(X:GetChildren())do m(d)end X.ChildAdded:Connect(m)D.OnClientEvent:Connect(function(a,d,T)B=T g()end)p.OnClientEvent:Connect(function(a,d,T)G[T]=nil if B==T then B=nil end end)
--[[
TheNexusAvenger

Controls hiding dungeons and teleporting the local player.
--]]

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

local ReplicatedStorageProject = require(ReplicatedStorage:WaitForChild("Project"):WaitForChild("ReplicatedStorage"))
local StartDungeon = ReplicatedStorageProject:GetResource("GameReplication.DungeonReplication.StartDungeon")
local ClearDungeon = ReplicatedStorageProject:GetResource("GameReplication.DungeonReplication.ClearDungeon")
local DungeonsContainer = Workspace:WaitForChild("Dungeons")

local StoredDungeons = {}
local CurrentDungeon



--[[
Teleports the player to the dungeon.
--]]
local function TeleportPlayerToDungeon()
    local Dungeon = StoredDungeons[CurrentDungeon]
    if Dungeon then
        local Character = Players.LocalPlayer.Character
        if Character then
            local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
            if HumanoidRootPart then
                local Spawn = Dungeon:WaitForChild("Spawn")
                local Rotation = CFrame.new(HumanoidRootPart.Position):inverse() * HumanoidRootPart.CFrame
                HumanoidRootPart.CFrame = Spawn.CFrame * CFrame.new(math.random() * (Spawn.Size.X/2),0,math.random() * (Spawn.Size.Z/2)) * Rotation
            end
        end
    end
end

--[[
Registers a new dungeon model.
--]]
local function DungeonAdded(Model)
    StoredDungeons[Model.Name] = Model
    if Model.Name == CurrentDungeon then
        TeleportPlayerToDungeon()
    end
end



--Add the dungeons.
for _,Model in pairs(DungeonsContainer:GetChildren()) do
    DungeonAdded(Model)
end
DungeonsContainer.ChildAdded:Connect(DungeonAdded)

--Connect the remote events.
StartDungeon.OnClientEvent:Connect(function(X,Y,Id)
    --Set the current dungeon.
    CurrentDungeon = Id

    --Teleport the player.
    TeleportPlayerToDungeon()
end)

ClearDungeon.OnClientEvent:Connect(function(X,Y,Id)
    StoredDungeons[Id] = nil
    if CurrentDungeon == Id then
        CurrentDungeon = nil
    end
end)