Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --wait for player and game to load in
- repeat wait() until game:IsLoaded()
- local player = game.Players.LocalPlayer
- local character = player.Character or player.CharacterAdded:Wait()
- local cam = workspace.CurrentCamera
- local rep = game.ReplicatedStorage
- --positional values to know if player is inside a specific building or not
- local inside : number = -575
- local outside : number = -494
- --ternary to know if player is using mobile or not
- local mobile = game:GetService("UserInputService"):GetLastInputType() == Enum.UserInputType.Focus and true or false
- local renderDistanceOption = player:WaitForChild("PlayerGui"):WaitForChild("Main"):WaitForChild("Settings"):WaitForChild("ScrollingFrame"):WaitForChild("Container"):WaitForChild("RenderDistance")
- local fps
- local spawned = false
- local enteredBuilding = false
- --just some instances cached
- local builds = workspace:WaitForChild("Builds")
- local interior = builds:WaitForChild("Interior")
- local exterior = builds:WaitForChild("Exterior")
- local level1Interior = builds:WaitForChild("Level1Walls")
- local level2Interior = builds:WaitForChild("Level2Walls")
- local level3Interior = builds:WaitForChild("Level3Walls")
- local levels = {}
- local currentLevel = 1
- local levelChanger1 = workspace:WaitForChild("LevelChanger1")
- local levelChanger2 = workspace:WaitForChild("LevelChanger2")
- local levelChanger3 = workspace:WaitForChild("LevelChanger3")
- --function to iterate through array of floors and change the floor's parent
- local function HideOtherStories()
- for level, floor in pairs(levels) do
- if level == currentLevel then continue end
- for _, room in pairs(floor) do
- room.Parent = rep
- end
- end
- end
- --reset the floors to standard when the player's character spawns in
- player.CharacterAdded:Connect(function(char)
- character = char
- currentLevel = 1
- level1Interior.Parent = builds
- level2Interior.Parent = rep
- level3Interior.Parent = rep
- HideOtherStories()
- end)
- --touched event on all floors to spawn/despawn correct floors
- levelChanger1.Touched:Connect(function(touched)
- if touched.Parent == character and currentLevel ~= 1 then
- for _, room in pairs(levels[currentLevel]) do
- room.Parent = rep
- end
- currentLevel = 1
- level1Interior.Parent = builds
- level2Interior.Parent = rep
- HideOtherStories()
- end
- end)
- levelChanger2.Touched:Connect(function(touched)
- if touched.Parent == character and currentLevel ~= 2 then
- for _, room in pairs(levels[currentLevel]) do
- room.Parent = rep
- end
- currentLevel = 2
- level1Interior.Parent = rep
- level3Interior.Parent = rep
- level2Interior.Parent = builds
- HideOtherStories()
- end
- end)
- levelChanger3.TouchEnded:Connect(function(touched)
- if touched.Parent == character and currentLevel ~= 3 then
- for _, room in pairs(levels[currentLevel]) do
- room.Parent = rep
- end
- currentLevel = 3
- level2Interior.Parent = rep
- level3Interior.Parent = builds
- end
- end)
- --just simply update the current displayed fps
- game:GetService("RunService").Heartbeat:Connect(function(delta)
- fps = 1/delta
- end)
- local function HideRooms()
- --get the rooms of the current floor
- for _, room in pairs(levels[currentLevel]) do
- --detect the distance to each room from player's camera
- local playerPos = cam.CFrame.Position
- local distToRoom = (playerPos - room.WorldPivot.Position).magnitude
- spawn/despawn each room if they are far away or close enough to player camera
- if(distToRoom < room:GetAttribute("RenderDistance")) then
- room.Parent = interior
- else
- room.Parent = rep
- end
- end
- end
- local function HideWalls()
- --walls are not parented the same way, so made separate function to hide/show them depending on which floor the player is on
- if currentLevel == 1 then
- level1Interior.Parent = builds
- level2Interior.Parent = rep
- level3Interior.Parent = rep
- end
- if currentLevel == 2 then
- level1Interior.Parent = rep
- level2Interior.Parent = builds
- level3Interior.Parent = rep
- end
- if currentLevel == 3 then
- level1Interior.Parent = rep
- level2Interior.Parent = rep
- level3Interior.Parent = builds
- end
- end
- local function HideExterior()
- local playerPos = cam.CFrame.Position
- --when player is inside the building, hide the exterior to remove a lot of models
- if playerPos.Z < outside then
- exterior.Parent = builds
- if enteredBuilding then
- enteredBuilding = false
- level1Interior.Parent = rep
- end
- else
- exterior.Parent = rep
- if not enteredBuilding then
- level1Interior.Parent = builds
- enteredBuilding = true
- end
- end
- end
- local function HideEverything()
- local playerPos = cam.CFrame.Position
- --player is currently outside, so hide all interior
- if playerPos.Z < inside then
- interior.Parent = rep
- level1Interior.Parent = rep
- level2Interior.Parent = rep
- level3Interior.Parent = rep
- return true
- end
- interior.Parent = builds
- return false
- end
- local function ShowEverything()
- --iterate through all floors and their rooms to show them (was used for intro scene)
- for level, rooms in pairs(levels) do
- for _, room in pairs(rooms) do
- room.Parent = interior
- end
- end
- level1Interior.Parent = builds
- level2Interior.Parent = builds
- level3Interior.Parent = builds
- interior.Parent = builds
- exterior.Parent = builds
- end
- ShowEverything()
- level1Interior.Parent = builds
- --following lines are to detect when player exist intro (bad code as previous developer made a bad system)
- player.PlayerValues.InMenu:GetPropertyChangedSignal("Value"):Wait()
- if rep:WaitForChild("PlayerData"):InvokeServer("GetData") then
- cam:GetPropertyChangedSignal("CameraType"):Wait()
- cam:GetPropertyChangedSignal("CameraType"):Wait()
- cam:GetPropertyChangedSignal("CameraType"):Wait()
- end
- level1Interior.Parent = rep
- level2Interior.Parent = rep
- level3Interior.Parent = rep
- --quick small algorithm to find all the floors and their rooms to cache them
- for _, level in pairs(interior:GetChildren()) do
- levels[tonumber(level.Name:sub(-1))] = level:GetChildren()
- for _, room in pairs(level:GetChildren()) do
- room.Parent = rep
- end
- level:Destroy()
- end
- level1Interior.Parent = builds
- --while loop that decides what to show and what to hide
- while wait(1) do
- if renderDistanceOption:GetAttribute("Enabled") then
- HideExterior()
- if not HideEverything() then
- HideRooms()
- HideWalls()
- end
- else
- ShowEverything()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement