Advertisement
IHATEMICROWAVEOVEN

The Fat Load

Jul 14th, 2022 (edited)
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.41 KB | None | 0 0
  1. local Chunks = {
  2. Chunk1 = {"Chunk2", "Chunk3", "Chunk5", "Chunk6", "Chunk9"},
  3. Chunk2 = {"Chunk1", "Chunk3", "Chunk4", "Chunk5", "Chunk6", "Chunk7", "Chunk10"},
  4. Chunk3 = {"Chunk1", "Chunk2", "Chunk4", "Chunk6", "Chunk7", "Chunk8", "Chunk11"},
  5. Chunk4 = {"Chunk2", "Chunk3", "Chunk7", "Chunk8", "Chunk12"},
  6. Chunk5 = {"Chunk1", "Chunk2", "Chunk6", "Chunk7", "Chunk9", "Chunk10", "Chunk13"},
  7. Chunk6 = {"Chunk1", "Chunk2", "Chunk3", "Chunk5", "Chunk7", "Chunk8", "Chunk9", "Chunk10", "Chunk11", "Chunk14"},
  8. Chunk7 = {"Chunk2", "Chunk3", "Chunk4", "Chunk5", "Chunk6", "Chunk8", "Chunk10", "Chunk11", "Chunk12", "Chunk15"},
  9. Chunk8 = {"Chunk3", "Chunk4", "Chunk6", "Chunk7", "Chunk11", "Chunk12", "Chunk16"},
  10. Chunk9 = {"Chunk1", "Chunk5", "Chunk6", "Chunk10", "Chunk11", "Chunk13", "Chunk14"},
  11. Chunk10 = {"Chunk2", "Chunk5", "Chunk6", "Chunk7", "Chunk9", "Chunk11", "Chunk12", "Chunk13", "Chunk14", "Chunk15"},
  12. Chunk11 = {"Chunk3", "Chunk6", "Chunk7", "Chunk8", "Chunk9", "Chunk10", "Chunk12", "Chunk14", "Chunk15", "Chunk16"},
  13. Chunk12 = {"Chunk4", "Chunk7", "Chunk8", "Chunk10", "Chunk11", "Chunk15", "Chunk16"},
  14. Chunk13 = {"Chunk5", "Chunk9", "Chunk10", "Chunk14", "Chunk15"},
  15. Chunk14 = {"Chunk6", "Chunk9", "Chunk10", "Chunk11", "Chunk13", "Chunk15", "Chunk16"},
  16. Chunk15 = {"Chunk7", "Chunk10", "Chunk11", "Chunk12", "Chunk13", "Chunk14", "Chunk16"},
  17. Chunk16 = {"Chunk8", "Chunk11", "Chunk12", "Chunk14", "Chunk15"}
  18. }
  19.  
  20. -- START TABLE LIST
  21.  
  22. local ReverseChunks = {}
  23. -- Stores the chunks we can see
  24. -- {Chunk1(string) = {{Chunk2(string) = true}, {Chunk3(string) = true}},
  25. -- etc}
  26.  
  27. local ChunkBounds = {}
  28. -- Identifies the start and end of each chunk
  29. -- {Chunk1(string) = {botx, botz, topx, topz},
  30. -- Chunk2(string) = {botx, botz, topx, topz},
  31. -- Chunk3(string) = {botx, botz, topx, topz},
  32. -- Chunk4(string) = {botx, botz, topx, topz},
  33. -- Chunk5(string) = {botx, botz, topx, topz},
  34. -- Chunk6(string) = {botx, botz, topx, topz},
  35. -- Chunk7(string) = {botx, botz, topx, topz}}
  36.  
  37. local ChunksVisible = {}
  38. -- Identifies visibility
  39. -- (trues can change to false)
  40. -- {Chunk1(string) = true,
  41. -- Chunk2(string) = true,
  42. -- Chunk3(string) = true,
  43. -- Chunk4(string) = true,
  44. -- Chunk5(string) = true,
  45. -- Chunk6(string) = true,
  46. -- Chunk7(string) = true}
  47.  
  48. local ChunkModels = {}
  49. -- Stores the models of chunks
  50. -- {Chunk1(string) = Chunk1(instance),
  51. -- Chunk2(string) = Chunk2(instance),
  52. -- Chunk3(string) = Chunk3(instance),
  53. -- Chunk4(string) = Chunk4(instance),
  54. -- Chunk5(string) = Chunk5(instance),
  55. -- Chunk6(string) = Chunk6(instance),
  56. -- Chunk7(string) = Chunk7(instance)}
  57.  
  58. local ChunkOBJs = {}
  59. -- Stores the chunk objects that "degrade"
  60. -- {Chunk1(string) = {Tree = {false, botx, botz, topx, topz, detailmodel, vaguemodel}},
  61. -- etc}
  62.  
  63. local LastChunk = "Chunk11"
  64.  
  65. -- END TABLE LIST
  66.  
  67. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  68. local ChunksModel = game.Workspace:WaitForChild("Chunks")
  69. local Camera = game.Workspace.CurrentCamera
  70. local HumanoidRootPart
  71.  
  72. for Name,ChunkTable in pairs(Chunks) do
  73. local Model = ChunksModel:WaitForChild(Name)
  74. ChunkModels[Name] = Model
  75. ChunksVisible[Name] = true
  76. ReverseChunks[Name] = {}
  77. ReverseChunks[Name][Name] = true
  78. for _,ChunkId in pairs(ChunkTable) do
  79. ReverseChunks[Name][ChunkId] = true
  80. end
  81. ChunkOBJs[Name] = {}
  82. if Model:FindFirstChild("DetailOBJs") and Model:FindFirstChild("VagueOBJs") then
  83. local Vagues = Model.VagueOBJs
  84. local Details = Model.DetailOBJs
  85. local RENDER = 100 -- How close to be for detailed objects? (higher = less close)
  86. for _, OBJ in pairs(Details:GetChildren()) do
  87. local MyPos = OBJ:GetPivot()
  88. local PosX,PosZ = MyPos.X,MyPos.Z
  89. local OBJfar = Vagues:FindFirstChild(tostring(OBJ))
  90. ChunkOBJs[Name][OBJ] = {false,PosX - RENDER,PosZ - RENDER,PosX + RENDER,PosZ + RENDER,OBJ,OBJfar}
  91. end
  92. end
  93.  
  94. local Position,Size = Model:GetModelCFrame().p,Model:GetExtentsSize()
  95. local PosX,PosZ = Position.X,Position.Z
  96. local SizeX,SizeZ = Size.X/2,Size.Z/2
  97. ChunkBounds[Name] = {PosX - SizeX,PosZ - SizeZ,PosX + SizeX,PosZ + SizeZ}
  98. end
  99. -- ^ Add everything to their own table
  100.  
  101. local function SwapOBJs(Chunk, PosX, PosZ)
  102. if ChunksVisible[Chunk] == false then
  103. for OBJName,OBJ in pairs(ChunkOBJs[Chunk]) do
  104. repeat OBJ[6].Parent = ReplicatedStorage until OBJ[6].Parent == ReplicatedStorage
  105. if OBJ[7] then repeat OBJ[7].Parent = ChunkModels[Chunk] until OBJ[7].Parent == ChunkModels[Chunk] end
  106. OBJ[1] = false
  107. return
  108. end
  109. end
  110. for OBJName,OBJ in pairs(ChunkOBJs[Chunk]) do
  111. local X1,Z1,X2,Z2 = OBJ[2],OBJ[3],OBJ[4],OBJ[5]
  112. if not (PosX and PosZ) then
  113. if not HumanoidRootPart then return end
  114. local Position = HumanoidRootPart.Position
  115. PosX,PosZ = Position.X,Position.Z
  116. end
  117.  
  118. if PosX > X1 and PosX < X2 and PosZ > Z1 and PosZ < Z2 then
  119. if OBJ[1] == false then
  120. repeat OBJ[6].Parent = ChunkModels[Chunk] until OBJ[6].Parent == ChunkModels[Chunk]
  121. if OBJ[7] then repeat OBJ[7].Parent = ReplicatedStorage until OBJ[7].Parent == ReplicatedStorage end
  122. OBJ[1] = true
  123. end
  124. elseif OBJ[1] == true then
  125. repeat OBJ[6].Parent = ReplicatedStorage until OBJ[6].Parent == ReplicatedStorage
  126. if OBJ[7] then repeat OBJ[7].Parent = ChunkModels[Chunk] until OBJ[7].Parent == ChunkModels[Chunk] end
  127. OBJ[1] = false
  128. end
  129. end
  130. end
  131. -- Changes the quality of objects in chunks
  132. -- If the chunk is invisible, automatically downgrade quality
  133. -- If the chunk is visible:
  134. -- If the player is near, upgrade (or maintain upgraded) quality
  135. -- If the player is far, downgrade (or maintain downgraded) quality
  136.  
  137. local function GetCameraInChunk(MyChunk)
  138. if not HumanoidRootPart then return end
  139. local Position = HumanoidRootPart.Position
  140. local PosX,PosZ = Position.X,Position.Z
  141.  
  142. local CurBounds = ChunkBounds[MyChunk]
  143. local X1,Z1,X2,Z2 = CurBounds[1],CurBounds[2],CurBounds[3],CurBounds[4]
  144. if PosX > X1 and PosX < X2 and PosZ > Z1 and PosZ < Z2 then
  145. return MyChunk,PosX,PosZ
  146. end
  147.  
  148. local Visibles = Chunks[MyChunk]
  149. local DoLast = {}
  150. for ChunkName,Bounds in pairs(ChunkBounds) do
  151. if Visibles[ChunkName] then
  152. X1,Z1,X2,Z2 = Bounds[1],Bounds[2],Bounds[3],Bounds[4]
  153. if PosX > X1 and PosX < X2 and PosZ > Z1 and PosZ < Z2 then
  154. return ChunkName,PosX,PosZ
  155. end
  156. else
  157. table.insert(DoLast,ChunkName)
  158. end
  159. end
  160.  
  161. for _,OtherChunk in pairs(DoLast) do
  162. local Bounds = ChunkBounds[OtherChunk]
  163. X1,Z1,X2,Z2 = Bounds[1],Bounds[2],Bounds[3],Bounds[4]
  164. if PosX > X1 and PosX < X2 and PosZ > Z1 and PosZ < Z2 then
  165. return OtherChunk,PosX,PosZ
  166. end
  167. end
  168.  
  169. return MyChunk
  170. end
  171. -- Identify the chunk the humanoid is in (by iterating through the chunks)
  172. -- Change made: has iteration "priority" (likely-to-be-nearer chunks are checked first)
  173.  
  174. local function SetChunkVisible(ChunkName,Visible)
  175. if Visible then
  176. repeat ChunkModels[ChunkName].Parent = ChunksModel until ChunkModels[ChunkName].Parent == ChunksModel
  177. ChunksVisible[ChunkName] = true
  178. else
  179. repeat ChunkModels[ChunkName].Parent = ReplicatedStorage until ChunkModels[ChunkName].Parent == ReplicatedStorage
  180. ChunksVisible[ChunkName] = false
  181. end
  182.  
  183. end
  184. -- If you want to make it visible:
  185. -- In ChunksVisible, identify it as visible
  186. -- Parent its model to the chunk holder
  187. -- If you want to make it invisible:
  188. -- In ChunksVisible, remove it
  189. -- Remove its model from the workspace
  190. -- (it's still remembered because we have the model in a table!)
  191.  
  192. local function UpdateRenderedChunks(ChunkName,PosX,PosZ)
  193. local ChunkData = ReverseChunks[ChunkName]
  194. for OtherChunkName,Visible in pairs(ChunksVisible) do
  195. if Visible == true and ChunkData[OtherChunkName] ~= true then
  196. SetChunkVisible(OtherChunkName,false)
  197. SwapOBJs(OtherChunkName,PosX,PosZ)
  198. end
  199. end
  200. for OtherChunkName,_ in pairs(ChunkData) do
  201. if ChunksVisible[OtherChunkName] ~= true then
  202. SetChunkVisible(OtherChunkName,true)
  203. SwapOBJs(OtherChunkName,PosX,PosZ)
  204. end
  205. end
  206. end
  207. -- First:
  208. -- For any other chunk that is visible but not in our chunk's reverse data...
  209. -- Set it invisible with the previous function
  210. -- Second:
  211. -- For every chunk that is invisible and in our chunk's reverse data...
  212. -- Set it visible with the previous function
  213. -- Third:
  214. -- If our chunk is invisible (in the ChunksVisible dict)...
  215. -- Set it visible with the previous function
  216. -- Load objects inside it
  217.  
  218.  
  219. -- THE ACTUAL SCRIPT vvvvvvv
  220.  
  221. local Player = game.Players.LocalPlayer
  222. local RunService = game:GetService("RunService")
  223. local pause = false
  224.  
  225. local function CharacterAdded(Character)
  226. if Character then
  227. HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
  228. end
  229. end
  230. CharacterAdded(Player.Character)
  231. Player.CharacterAdded:Connect(CharacterAdded)
  232. -- When our player spawns, identify their HumanoidRootPart (for bounds system)
  233.  
  234. local function LoadEm()
  235. local ChunkName,PosX,PosZ = GetCameraInChunk(LastChunk) -- Identify our current chunk
  236. if ChunkName and ChunkName ~= LastChunk then -- If we're in a new chunk:
  237. LastChunk = ChunkName -- Remember this new chunk
  238. UpdateRenderedChunks(ChunkName,PosX,PosZ) -- Load the new chunk
  239. else -- Otherwise:
  240. SwapOBJs(LastChunk,PosX,PosZ) -- Sort objects as per usual
  241. end
  242. end
  243.  
  244. RunService.RenderStepped:Connect(function()
  245. if coroutine.running() ~= LoadEm() and pause == false then LoadEm() end
  246. end)
  247.  
  248.  
  249.  
  250.  
  251. -- Everything below here is used for TP systems.
  252.  
  253. local function TPLoading(NewChunk)
  254. if not ChunkModels[NewChunk] then return false end
  255. if pause then warn("Called chunk loading requests too fast!") return false end
  256. pause = true
  257. repeat RunService.RenderStepped:Wait() until coroutine.running() ~= LoadEm()
  258. UpdateRenderedChunks(NewChunk)
  259. pause = false
  260. return true
  261. end
  262.  
  263. local BindFunc = script.Parent:WaitForChild("ClientChunkRequest")
  264. BindFunc.OnInvoke = TPLoading
  265. -- To utilize the above function, create a BindableFunction named ClientChunkRequest
  266. -- in StarterPlayerScripts. Then, in a client-side script, write:
  267. -- if game.Players.LocalPlayer.PlayerScripts.ClientChunkRequest:Invoke([chunk]) == true then print("eez") end
  268.  
  269. local RemoteEvent = ReplicatedStorage:WaitForChild("ChunkRequest")
  270. RemoteEvent.OnClientInvoke = TPLoading
  271. -- To utilize the above function, create a RemoteFunction named ChunkRequest
  272. -- in ReplicatedStorage. Then, in a server-side script, write:
  273. -- if game.ReplicatedStorage.ChunkRequest:InvokeClient([player],[chunk]) then print("buff registeel") end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement