Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local Chunks = {
- Chunk1 = {"Chunk2", "Chunk3", "Chunk5", "Chunk6", "Chunk9"},
- Chunk2 = {"Chunk1", "Chunk3", "Chunk4", "Chunk5", "Chunk6", "Chunk7", "Chunk10"},
- Chunk3 = {"Chunk1", "Chunk2", "Chunk4", "Chunk6", "Chunk7", "Chunk8", "Chunk11"},
- Chunk4 = {"Chunk2", "Chunk3", "Chunk7", "Chunk8", "Chunk12"},
- Chunk5 = {"Chunk1", "Chunk2", "Chunk6", "Chunk7", "Chunk9", "Chunk10", "Chunk13"},
- Chunk6 = {"Chunk1", "Chunk2", "Chunk3", "Chunk5", "Chunk7", "Chunk8", "Chunk9", "Chunk10", "Chunk11", "Chunk14"},
- Chunk7 = {"Chunk2", "Chunk3", "Chunk4", "Chunk5", "Chunk6", "Chunk8", "Chunk10", "Chunk11", "Chunk12", "Chunk15"},
- Chunk8 = {"Chunk3", "Chunk4", "Chunk6", "Chunk7", "Chunk11", "Chunk12", "Chunk16"},
- Chunk9 = {"Chunk1", "Chunk5", "Chunk6", "Chunk10", "Chunk11", "Chunk13", "Chunk14"},
- Chunk10 = {"Chunk2", "Chunk5", "Chunk6", "Chunk7", "Chunk9", "Chunk11", "Chunk12", "Chunk13", "Chunk14", "Chunk15"},
- Chunk11 = {"Chunk3", "Chunk6", "Chunk7", "Chunk8", "Chunk9", "Chunk10", "Chunk12", "Chunk14", "Chunk15", "Chunk16"},
- Chunk12 = {"Chunk4", "Chunk7", "Chunk8", "Chunk10", "Chunk11", "Chunk15", "Chunk16"},
- Chunk13 = {"Chunk5", "Chunk9", "Chunk10", "Chunk14", "Chunk15"},
- Chunk14 = {"Chunk6", "Chunk9", "Chunk10", "Chunk11", "Chunk13", "Chunk15", "Chunk16"},
- Chunk15 = {"Chunk7", "Chunk10", "Chunk11", "Chunk12", "Chunk13", "Chunk14", "Chunk16"},
- Chunk16 = {"Chunk8", "Chunk11", "Chunk12", "Chunk14", "Chunk15"}
- }
- -- START TABLE LIST
- local ReverseChunks = {}
- -- Stores the chunks we can see
- -- {Chunk1(string) = {{Chunk2(string) = true}, {Chunk3(string) = true}},
- -- etc}
- local ChunkBounds = {}
- -- Identifies the start and end of each chunk
- -- {Chunk1(string) = {botx, botz, topx, topz},
- -- Chunk2(string) = {botx, botz, topx, topz},
- -- Chunk3(string) = {botx, botz, topx, topz},
- -- Chunk4(string) = {botx, botz, topx, topz},
- -- Chunk5(string) = {botx, botz, topx, topz},
- -- Chunk6(string) = {botx, botz, topx, topz},
- -- Chunk7(string) = {botx, botz, topx, topz}}
- local ChunksVisible = {}
- -- Identifies visibility
- -- (trues can change to false)
- -- {Chunk1(string) = true,
- -- Chunk2(string) = true,
- -- Chunk3(string) = true,
- -- Chunk4(string) = true,
- -- Chunk5(string) = true,
- -- Chunk6(string) = true,
- -- Chunk7(string) = true}
- local ChunkModels = {}
- -- Stores the models of chunks
- -- {Chunk1(string) = Chunk1(instance),
- -- Chunk2(string) = Chunk2(instance),
- -- Chunk3(string) = Chunk3(instance),
- -- Chunk4(string) = Chunk4(instance),
- -- Chunk5(string) = Chunk5(instance),
- -- Chunk6(string) = Chunk6(instance),
- -- Chunk7(string) = Chunk7(instance)}
- local ChunkOBJs = {}
- -- Stores the chunk objects that "degrade"
- -- {Chunk1(string) = {Tree = {false, botx, botz, topx, topz, detailmodel, vaguemodel}},
- -- etc}
- local LastChunk = "Chunk11"
- -- END TABLE LIST
- local ReplicatedStorage = game:GetService("ReplicatedStorage")
- local ChunksModel = game.Workspace:WaitForChild("Chunks")
- local Camera = game.Workspace.CurrentCamera
- local HumanoidRootPart
- for Name,ChunkTable in pairs(Chunks) do
- local Model = ChunksModel:WaitForChild(Name)
- ChunkModels[Name] = Model
- ChunksVisible[Name] = true
- ReverseChunks[Name] = {}
- ReverseChunks[Name][Name] = true
- for _,ChunkId in pairs(ChunkTable) do
- ReverseChunks[Name][ChunkId] = true
- end
- ChunkOBJs[Name] = {}
- if Model:FindFirstChild("DetailOBJs") and Model:FindFirstChild("VagueOBJs") then
- local Vagues = Model.VagueOBJs
- local Details = Model.DetailOBJs
- local RENDER = 100 -- How close to be for detailed objects? (higher = less close)
- for _, OBJ in pairs(Details:GetChildren()) do
- local MyPos = OBJ:GetPivot()
- local PosX,PosZ = MyPos.X,MyPos.Z
- local OBJfar = Vagues:FindFirstChild(tostring(OBJ))
- ChunkOBJs[Name][OBJ] = {false,PosX - RENDER,PosZ - RENDER,PosX + RENDER,PosZ + RENDER,OBJ,OBJfar}
- end
- end
- local Position,Size = Model:GetModelCFrame().p,Model:GetExtentsSize()
- local PosX,PosZ = Position.X,Position.Z
- local SizeX,SizeZ = Size.X/2,Size.Z/2
- ChunkBounds[Name] = {PosX - SizeX,PosZ - SizeZ,PosX + SizeX,PosZ + SizeZ}
- end
- -- ^ Add everything to their own table
- local function SwapOBJs(Chunk, PosX, PosZ)
- if ChunksVisible[Chunk] == false then
- for OBJName,OBJ in pairs(ChunkOBJs[Chunk]) do
- repeat OBJ[6].Parent = ReplicatedStorage until OBJ[6].Parent == ReplicatedStorage
- if OBJ[7] then repeat OBJ[7].Parent = ChunkModels[Chunk] until OBJ[7].Parent == ChunkModels[Chunk] end
- OBJ[1] = false
- return
- end
- end
- for OBJName,OBJ in pairs(ChunkOBJs[Chunk]) do
- local X1,Z1,X2,Z2 = OBJ[2],OBJ[3],OBJ[4],OBJ[5]
- if not (PosX and PosZ) then
- if not HumanoidRootPart then return end
- local Position = HumanoidRootPart.Position
- PosX,PosZ = Position.X,Position.Z
- end
- if PosX > X1 and PosX < X2 and PosZ > Z1 and PosZ < Z2 then
- if OBJ[1] == false then
- repeat OBJ[6].Parent = ChunkModels[Chunk] until OBJ[6].Parent == ChunkModels[Chunk]
- if OBJ[7] then repeat OBJ[7].Parent = ReplicatedStorage until OBJ[7].Parent == ReplicatedStorage end
- OBJ[1] = true
- end
- elseif OBJ[1] == true then
- repeat OBJ[6].Parent = ReplicatedStorage until OBJ[6].Parent == ReplicatedStorage
- if OBJ[7] then repeat OBJ[7].Parent = ChunkModels[Chunk] until OBJ[7].Parent == ChunkModels[Chunk] end
- OBJ[1] = false
- end
- end
- end
- -- Changes the quality of objects in chunks
- -- If the chunk is invisible, automatically downgrade quality
- -- If the chunk is visible:
- -- If the player is near, upgrade (or maintain upgraded) quality
- -- If the player is far, downgrade (or maintain downgraded) quality
- local function GetCameraInChunk(MyChunk)
- if not HumanoidRootPart then return end
- local Position = HumanoidRootPart.Position
- local PosX,PosZ = Position.X,Position.Z
- local CurBounds = ChunkBounds[MyChunk]
- local X1,Z1,X2,Z2 = CurBounds[1],CurBounds[2],CurBounds[3],CurBounds[4]
- if PosX > X1 and PosX < X2 and PosZ > Z1 and PosZ < Z2 then
- return MyChunk,PosX,PosZ
- end
- local Visibles = Chunks[MyChunk]
- local DoLast = {}
- for ChunkName,Bounds in pairs(ChunkBounds) do
- if Visibles[ChunkName] then
- X1,Z1,X2,Z2 = Bounds[1],Bounds[2],Bounds[3],Bounds[4]
- if PosX > X1 and PosX < X2 and PosZ > Z1 and PosZ < Z2 then
- return ChunkName,PosX,PosZ
- end
- else
- table.insert(DoLast,ChunkName)
- end
- end
- for _,OtherChunk in pairs(DoLast) do
- local Bounds = ChunkBounds[OtherChunk]
- X1,Z1,X2,Z2 = Bounds[1],Bounds[2],Bounds[3],Bounds[4]
- if PosX > X1 and PosX < X2 and PosZ > Z1 and PosZ < Z2 then
- return OtherChunk,PosX,PosZ
- end
- end
- return MyChunk
- end
- -- Identify the chunk the humanoid is in (by iterating through the chunks)
- -- Change made: has iteration "priority" (likely-to-be-nearer chunks are checked first)
- local function SetChunkVisible(ChunkName,Visible)
- if Visible then
- repeat ChunkModels[ChunkName].Parent = ChunksModel until ChunkModels[ChunkName].Parent == ChunksModel
- ChunksVisible[ChunkName] = true
- else
- repeat ChunkModels[ChunkName].Parent = ReplicatedStorage until ChunkModels[ChunkName].Parent == ReplicatedStorage
- ChunksVisible[ChunkName] = false
- end
- end
- -- If you want to make it visible:
- -- In ChunksVisible, identify it as visible
- -- Parent its model to the chunk holder
- -- If you want to make it invisible:
- -- In ChunksVisible, remove it
- -- Remove its model from the workspace
- -- (it's still remembered because we have the model in a table!)
- local function UpdateRenderedChunks(ChunkName,PosX,PosZ)
- local ChunkData = ReverseChunks[ChunkName]
- for OtherChunkName,Visible in pairs(ChunksVisible) do
- if Visible == true and ChunkData[OtherChunkName] ~= true then
- SetChunkVisible(OtherChunkName,false)
- SwapOBJs(OtherChunkName,PosX,PosZ)
- end
- end
- for OtherChunkName,_ in pairs(ChunkData) do
- if ChunksVisible[OtherChunkName] ~= true then
- SetChunkVisible(OtherChunkName,true)
- SwapOBJs(OtherChunkName,PosX,PosZ)
- end
- end
- end
- -- First:
- -- For any other chunk that is visible but not in our chunk's reverse data...
- -- Set it invisible with the previous function
- -- Second:
- -- For every chunk that is invisible and in our chunk's reverse data...
- -- Set it visible with the previous function
- -- Third:
- -- If our chunk is invisible (in the ChunksVisible dict)...
- -- Set it visible with the previous function
- -- Load objects inside it
- -- THE ACTUAL SCRIPT vvvvvvv
- local Player = game.Players.LocalPlayer
- local RunService = game:GetService("RunService")
- local pause = false
- local function CharacterAdded(Character)
- if Character then
- HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
- end
- end
- CharacterAdded(Player.Character)
- Player.CharacterAdded:Connect(CharacterAdded)
- -- When our player spawns, identify their HumanoidRootPart (for bounds system)
- local function LoadEm()
- local ChunkName,PosX,PosZ = GetCameraInChunk(LastChunk) -- Identify our current chunk
- if ChunkName and ChunkName ~= LastChunk then -- If we're in a new chunk:
- LastChunk = ChunkName -- Remember this new chunk
- UpdateRenderedChunks(ChunkName,PosX,PosZ) -- Load the new chunk
- else -- Otherwise:
- SwapOBJs(LastChunk,PosX,PosZ) -- Sort objects as per usual
- end
- end
- RunService.RenderStepped:Connect(function()
- if coroutine.running() ~= LoadEm() and pause == false then LoadEm() end
- end)
- -- Everything below here is used for TP systems.
- local function TPLoading(NewChunk)
- if not ChunkModels[NewChunk] then return false end
- if pause then warn("Called chunk loading requests too fast!") return false end
- pause = true
- repeat RunService.RenderStepped:Wait() until coroutine.running() ~= LoadEm()
- UpdateRenderedChunks(NewChunk)
- pause = false
- return true
- end
- local BindFunc = script.Parent:WaitForChild("ClientChunkRequest")
- BindFunc.OnInvoke = TPLoading
- -- To utilize the above function, create a BindableFunction named ClientChunkRequest
- -- in StarterPlayerScripts. Then, in a client-side script, write:
- -- if game.Players.LocalPlayer.PlayerScripts.ClientChunkRequest:Invoke([chunk]) == true then print("eez") end
- local RemoteEvent = ReplicatedStorage:WaitForChild("ChunkRequest")
- RemoteEvent.OnClientInvoke = TPLoading
- -- To utilize the above function, create a RemoteFunction named ChunkRequest
- -- in ReplicatedStorage. Then, in a server-side script, write:
- -- if game.ReplicatedStorage.ChunkRequest:InvokeClient([player],[chunk]) then print("buff registeel") end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement