Advertisement
world_killer

game optimizer

Sep 13th, 2023 (edited)
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.40 KB | Source Code | 0 0
  1. --wait for player and game to load in
  2. repeat wait() until game:IsLoaded()
  3. local player = game.Players.LocalPlayer
  4. local character = player.Character or player.CharacterAdded:Wait()
  5.  
  6. local cam = workspace.CurrentCamera
  7. local rep = game.ReplicatedStorage
  8.  
  9. --positional values to know if player is inside a specific building or not
  10. local inside : number = -575
  11. local outside : number = -494
  12.  
  13. --ternary to know if player is using mobile or not
  14. local mobile = game:GetService("UserInputService"):GetLastInputType() == Enum.UserInputType.Focus and true or false
  15.  
  16. local renderDistanceOption = player:WaitForChild("PlayerGui"):WaitForChild("Main"):WaitForChild("Settings"):WaitForChild("ScrollingFrame"):WaitForChild("Container"):WaitForChild("RenderDistance")
  17.  
  18. local fps
  19. local spawned = false
  20. local enteredBuilding = false
  21.  
  22. --just some instances cached
  23. local builds = workspace:WaitForChild("Builds")
  24. local interior = builds:WaitForChild("Interior")
  25. local exterior = builds:WaitForChild("Exterior")
  26.  
  27. local level1Interior = builds:WaitForChild("Level1Walls")
  28. local level2Interior = builds:WaitForChild("Level2Walls")
  29. local level3Interior = builds:WaitForChild("Level3Walls")
  30.  
  31.  
  32. local levels = {}
  33. local currentLevel = 1
  34.  
  35. local levelChanger1 = workspace:WaitForChild("LevelChanger1")
  36. local levelChanger2 = workspace:WaitForChild("LevelChanger2")
  37. local levelChanger3 = workspace:WaitForChild("LevelChanger3")
  38.  
  39. --function to iterate through array of floors and change the floor's parent
  40. local function HideOtherStories()
  41.     for level, floor in pairs(levels) do
  42.         if level == currentLevel then continue end
  43.        
  44.         for _, room in pairs(floor) do
  45.             room.Parent = rep
  46.         end
  47.     end
  48. end
  49.  
  50. --reset the floors to standard when the player's character spawns in
  51. player.CharacterAdded:Connect(function(char)
  52.     character = char
  53.     currentLevel = 1
  54.    
  55.     level1Interior.Parent = builds
  56.     level2Interior.Parent = rep
  57.     level3Interior.Parent = rep
  58.     HideOtherStories()
  59. end)
  60.  
  61. --touched event on all floors to spawn/despawn correct floors
  62. levelChanger1.Touched:Connect(function(touched)
  63.     if touched.Parent == character and currentLevel ~= 1 then
  64.         for _, room in pairs(levels[currentLevel]) do
  65.             room.Parent = rep
  66.         end
  67.        
  68.         currentLevel = 1
  69.         level1Interior.Parent = builds
  70.         level2Interior.Parent = rep
  71.         HideOtherStories()
  72.     end
  73. end)
  74.  
  75. levelChanger2.Touched:Connect(function(touched)
  76.     if touched.Parent == character and currentLevel ~= 2 then
  77.         for _, room in pairs(levels[currentLevel]) do
  78.             room.Parent = rep
  79.         end
  80.        
  81.         currentLevel = 2
  82.         level1Interior.Parent = rep
  83.         level3Interior.Parent = rep
  84.         level2Interior.Parent = builds
  85.         HideOtherStories()
  86.     end
  87. end)
  88.  
  89. levelChanger3.TouchEnded:Connect(function(touched)
  90.     if touched.Parent == character and currentLevel ~= 3 then
  91.         for _, room in pairs(levels[currentLevel]) do
  92.             room.Parent = rep
  93.         end
  94.        
  95.         currentLevel = 3
  96.         level2Interior.Parent = rep
  97.         level3Interior.Parent = builds
  98.     end
  99. end)
  100.  
  101. --just simply update the current displayed fps
  102. game:GetService("RunService").Heartbeat:Connect(function(delta)
  103.     fps = 1/delta
  104. end)
  105.  
  106. local function HideRooms()
  107. --get the rooms of the current floor
  108.     for _, room in pairs(levels[currentLevel]) do
  109.        
  110.         --detect the distance to each room from player's camera
  111.         local playerPos = cam.CFrame.Position
  112.         local distToRoom = (playerPos - room.WorldPivot.Position).magnitude
  113.        
  114.         spawn/despawn each room if they are far away or close enough to player camera
  115.         if(distToRoom < room:GetAttribute("RenderDistance")) then
  116.             room.Parent = interior
  117.         else
  118.             room.Parent = rep
  119.         end
  120.     end
  121. end
  122.  
  123. local function HideWalls()
  124.     --walls are not parented the same way, so made separate function to hide/show them depending on which floor the player is on
  125.     if currentLevel == 1 then
  126.         level1Interior.Parent = builds
  127.         level2Interior.Parent = rep
  128.         level3Interior.Parent = rep
  129.     end
  130.    
  131.     if currentLevel == 2 then
  132.         level1Interior.Parent = rep
  133.         level2Interior.Parent = builds
  134.         level3Interior.Parent = rep
  135.     end
  136.    
  137.     if currentLevel == 3 then
  138.         level1Interior.Parent = rep
  139.         level2Interior.Parent = rep
  140.         level3Interior.Parent = builds
  141.     end
  142. end
  143.  
  144. local function HideExterior()
  145.     local playerPos = cam.CFrame.Position
  146.     --when player is inside the building, hide the exterior to remove a lot of models
  147.     if playerPos.Z < outside then
  148.         exterior.Parent = builds
  149.        
  150.         if enteredBuilding then
  151.             enteredBuilding = false
  152.             level1Interior.Parent = rep
  153.         end
  154.     else
  155.         exterior.Parent = rep
  156.        
  157.         if not enteredBuilding then
  158.             level1Interior.Parent = builds
  159.             enteredBuilding = true
  160.         end
  161.     end
  162. end
  163.  
  164. local function HideEverything()
  165.     local playerPos = cam.CFrame.Position
  166.     --player is currently outside, so hide all interior
  167.     if playerPos.Z < inside then
  168.         interior.Parent = rep
  169.         level1Interior.Parent = rep
  170.         level2Interior.Parent = rep
  171.         level3Interior.Parent = rep
  172.         return true
  173.     end
  174.    
  175.     interior.Parent = builds
  176.     return false
  177. end
  178.  
  179. local function ShowEverything()
  180.     --iterate through all floors and their rooms to show them (was used for intro scene)
  181.     for level, rooms in pairs(levels) do
  182.         for _, room in pairs(rooms) do
  183.             room.Parent = interior
  184.         end
  185.     end
  186.    
  187.     level1Interior.Parent = builds
  188.     level2Interior.Parent = builds
  189.     level3Interior.Parent = builds
  190.    
  191.     interior.Parent = builds
  192.     exterior.Parent = builds
  193. end
  194.  
  195. ShowEverything()
  196. level1Interior.Parent = builds
  197. --following lines are to detect when player exist intro (bad code as previous developer made a bad system)
  198. player.PlayerValues.InMenu:GetPropertyChangedSignal("Value"):Wait()
  199.  
  200. if rep:WaitForChild("PlayerData"):InvokeServer("GetData") then
  201.     cam:GetPropertyChangedSignal("CameraType"):Wait()
  202.     cam:GetPropertyChangedSignal("CameraType"):Wait()
  203.     cam:GetPropertyChangedSignal("CameraType"):Wait()
  204. end
  205.  
  206. level1Interior.Parent = rep
  207. level2Interior.Parent = rep
  208. level3Interior.Parent = rep
  209.  
  210. --quick small algorithm to find all the floors and their rooms to cache them
  211. for _, level in pairs(interior:GetChildren()) do
  212.     levels[tonumber(level.Name:sub(-1))] = level:GetChildren()
  213.  
  214.     for _, room in pairs(level:GetChildren()) do
  215.         room.Parent = rep
  216.     end
  217.  
  218.     level:Destroy()
  219. end
  220. level1Interior.Parent = builds
  221.  
  222. --while loop that decides what to show and what to hide
  223. while wait(1) do
  224.     if renderDistanceOption:GetAttribute("Enabled") then
  225.         HideExterior()
  226.    
  227.         if not HideEverything() then
  228.             HideRooms()
  229.             HideWalls()
  230.         end
  231.     else
  232.         ShowEverything()
  233.     end
  234. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement