Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local Chunks = {
- Chunk1 = {"Chunk2","Chunk3"},
- Chunk2 = {"Chunk1","Chunk4","Chunk6"},
- Chunk3 = {"Chunk1","Chunk4","Chunk5"},
- Chunk4 = {"Chunk2","Chunk3","Chunk5","Chunk6"},
- Chunk5 = {"Chunk3","Chunk4","Chunk7"},
- Chunk6 = {"Chunk2","Chunk4","Chunk7"},
- Chunk7 = {"Chunk5","Chunk6"},
- }
- -- 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 LastChunk = ""
- -- END TABLE LIST
- 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] = {}
- for _,ChunkId in pairs(ChunkTable) do
- ReverseChunks[Name][ChunkId] = true
- 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 GetCameraInChunk()
- if not HumanoidRootPart then return end
- local Position = HumanoidRootPart.Position
- local PosX,PosZ = Position.X,Position.Z
- for ChunkName,Bounds in pairs(ChunkBounds) do
- local X1,Z1,X2,Z2 = Bounds[1],Bounds[2],Bounds[3],Bounds[4]
- if PosX > X1 and PosZ > Z1 and PosX < X2 and PosZ < Z2 then
- return ChunkName
- end
- end
- end
- -- Identify the chunk the humanoid is in (by iterating through the chunks)
- local function SetChunkVisible(ChunkName,Visible)
- ChunksVisible[ChunkName] = (Visible == true and true or nil)
- ChunkModels[ChunkName].Parent = (Visible == true and ChunksModel or nil)
- 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)
- local ChunkData = ReverseChunks[ChunkName]
- for OtherChunkName,Visible in pairs(ChunksVisible) do
- if Visible == true and ChunkData[OtherChunkName] ~= true then
- SetChunkVisible(OtherChunkName,false)
- end
- end
- for OtherChunkName,_ in pairs(ChunkData) do
- if ChunksVisible[OtherChunkName] ~= true then
- SetChunkVisible(OtherChunkName,true)
- end
- end
- if ChunksVisible[ChunkName] ~= true then
- SetChunkVisible(ChunkName,true)
- 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
- -- THE ACTUAL SCRIPT vvvvvvv
- local Player = game.Players.LocalPlayer
- 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)
- while true do
- local ChunkName = GetCameraInChunk() -- 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) -- Load the new chunk
- end
- wait() -- Goddamnit
- end
Add Comment
Please, Sign In to add comment