IHATEMICROWAVEOVEN

chunk loading to read

Jun 13th, 2022 (edited)
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.66 KB | None | 0 0
  1. local Chunks = {
  2. Chunk1 = {"Chunk2","Chunk3"},
  3. Chunk2 = {"Chunk1","Chunk4","Chunk6"},
  4. Chunk3 = {"Chunk1","Chunk4","Chunk5"},
  5. Chunk4 = {"Chunk2","Chunk3","Chunk5","Chunk6"},
  6. Chunk5 = {"Chunk3","Chunk4","Chunk7"},
  7. Chunk6 = {"Chunk2","Chunk4","Chunk7"},
  8. Chunk7 = {"Chunk5","Chunk6"},
  9.  
  10. }
  11.  
  12. -- START TABLE LIST
  13.  
  14. local ReverseChunks = {}
  15. -- Stores the chunks we can see
  16. -- {Chunk1(string) = {{Chunk2(string) = true}, {Chunk3(string) = true}},
  17. -- etc}
  18.  
  19. local ChunkBounds = {}
  20. -- Identifies the start and end of each chunk
  21. -- {Chunk1(string) = {botx, botz, topx, topz},
  22. -- Chunk2(string) = {botx, botz, topx, topz},
  23. -- Chunk3(string) = {botx, botz, topx, topz},
  24. -- Chunk4(string) = {botx, botz, topx, topz},
  25. -- Chunk5(string) = {botx, botz, topx, topz},
  26. -- Chunk6(string) = {botx, botz, topx, topz},
  27. -- Chunk7(string) = {botx, botz, topx, topz}}
  28.  
  29. local ChunksVisible = {}
  30. -- Identifies visibility
  31. -- (trues can change to false)
  32. -- {Chunk1(string) = true,
  33. -- Chunk2(string) = true,
  34. -- Chunk3(string) = true,
  35. -- Chunk4(string) = true,
  36. -- Chunk5(string) = true,
  37. -- Chunk6(string) = true,
  38. -- Chunk7(string) = true}
  39.  
  40. local ChunkModels = {}
  41. -- Stores the models of chunks
  42. -- {Chunk1(string) = Chunk1(instance),
  43. -- Chunk2(string) = Chunk2(instance),
  44. -- Chunk3(string) = Chunk3(instance),
  45. -- Chunk4(string) = Chunk4(instance),
  46. -- Chunk5(string) = Chunk5(instance),
  47. -- Chunk6(string) = Chunk6(instance),
  48. -- Chunk7(string) = Chunk7(instance)}
  49.  
  50. local LastChunk = ""
  51.  
  52. -- END TABLE LIST
  53.  
  54. local ChunksModel = game.Workspace:WaitForChild("Chunks")
  55. local Camera = game.Workspace.CurrentCamera
  56. local HumanoidRootPart
  57.  
  58. for Name,ChunkTable in pairs(Chunks) do
  59. local Model = ChunksModel:WaitForChild(Name)
  60. ChunkModels[Name] = Model
  61. ChunksVisible[Name] = true
  62. ReverseChunks[Name] = {}
  63. for _,ChunkId in pairs(ChunkTable) do
  64. ReverseChunks[Name][ChunkId] = true
  65. end
  66.  
  67. local Position,Size = Model:GetModelCFrame().p,Model:GetExtentsSize()
  68. local PosX,PosZ = Position.X,Position.Z
  69. local SizeX,SizeZ = Size.X/2,Size.Z/2
  70. ChunkBounds[Name] = {PosX - SizeX,PosZ - SizeZ,PosX + SizeX,PosZ + SizeZ}
  71. end
  72. -- ^ Add everything to their own table
  73.  
  74. local function GetCameraInChunk()
  75. if not HumanoidRootPart then return end
  76. local Position = HumanoidRootPart.Position
  77. local PosX,PosZ = Position.X,Position.Z
  78. for ChunkName,Bounds in pairs(ChunkBounds) do
  79. local X1,Z1,X2,Z2 = Bounds[1],Bounds[2],Bounds[3],Bounds[4]
  80. if PosX > X1 and PosZ > Z1 and PosX < X2 and PosZ < Z2 then
  81. return ChunkName
  82. end
  83. end
  84. end
  85. -- Identify the chunk the humanoid is in (by iterating through the chunks)
  86.  
  87. local function SetChunkVisible(ChunkName,Visible)
  88. ChunksVisible[ChunkName] = (Visible == true and true or nil)
  89. ChunkModels[ChunkName].Parent = (Visible == true and ChunksModel or nil)
  90. end
  91. -- If you want to make it visible:
  92. -- In ChunksVisible, identify it as visible
  93. -- Parent its model to the chunk holder
  94. -- If you want to make it invisible:
  95. -- In ChunksVisible, remove it
  96. -- Remove its model from the workspace
  97. -- (it's still remembered because we have the model in a table!)
  98.  
  99. local function UpdateRenderedChunks(ChunkName)
  100. local ChunkData = ReverseChunks[ChunkName]
  101. for OtherChunkName,Visible in pairs(ChunksVisible) do
  102. if Visible == true and ChunkData[OtherChunkName] ~= true then
  103. SetChunkVisible(OtherChunkName,false)
  104. end
  105. end
  106.  
  107. for OtherChunkName,_ in pairs(ChunkData) do
  108. if ChunksVisible[OtherChunkName] ~= true then
  109. SetChunkVisible(OtherChunkName,true)
  110. end
  111. end
  112.  
  113. if ChunksVisible[ChunkName] ~= true then
  114. SetChunkVisible(ChunkName,true)
  115. end
  116. end
  117. -- First:
  118. -- For any other chunk that is visible but not in our chunk's reverse data...
  119. -- Set it invisible with the previous function
  120. -- Second:
  121. -- For every chunk that is invisible and in our chunk's reverse data...
  122. -- Set it visible with the previous function
  123. -- Third:
  124. -- If our chunk is invisible (in the ChunksVisible dict)...
  125. -- Set it visible with the previous function
  126.  
  127.  
  128. -- THE ACTUAL SCRIPT vvvvvvv
  129.  
  130. local Player = game.Players.LocalPlayer
  131. local function CharacterAdded(Character)
  132. if Character then
  133. HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
  134. end
  135. end
  136. CharacterAdded(Player.Character)
  137. Player.CharacterAdded:connect(CharacterAdded)
  138. -- When our player spawns, identify their HumanoidRootPart (for bounds system)
  139.  
  140. while true do
  141. local ChunkName = GetCameraInChunk() -- Identify our current chunk
  142. if ChunkName and ChunkName ~= LastChunk then -- If we're in a new chunk:
  143. LastChunk = ChunkName -- Remember this new chunk
  144. UpdateRenderedChunks(ChunkName) -- Load the new chunk
  145. end
  146. wait() -- Goddamnit
  147. end
Add Comment
Please, Sign In to add comment