Advertisement
MysteriaFool

Script

Apr 16th, 2025
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.96 KB | None | 0 0
  1. -- Grab The Services
  2. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  3. local Players = game:GetService("Players")
  4. local Workspace = game:GetService("Workspace")
  5. local RunService = game:GetService("RunService")
  6. local TweenService = game:GetService("TweenService")
  7.  
  8.  
  9. -- Main Obby module
  10. local Obby = {}
  11.  
  12. -- Runs when the game starts
  13. function Obby.OnStart()
  14.     -- Set up all the special parts of our obby
  15.     Obby.CreateKillParts()
  16.     Obby.CreateSpawnLocations()
  17.     Obby.CreateFakePlatforms()
  18.     Obby.CreateMovingPlatforms()
  19.     Obby.CreateChoiceDoors()
  20.  
  21.     -- When a new player joins
  22.     Players.PlayerAdded:Connect(function(player)
  23.         -- Make sure they have leaderstats
  24.         if not player:FindFirstChild("leaderstats") then
  25.             Obby.CreateLeaderstats(player)
  26.         end
  27.        
  28.         -- When their character spawns in
  29.         player.CharacterAdded:Connect(function(character)
  30.             -- Store when they spawned (used for checkpoint delay)
  31.             character:SetAttribute("SpawnTime", tick())
  32.  
  33.             -- Send them to the right spawn for their current stage
  34.             Obby.HandlePlayerRespawn(player)
  35.  
  36.             -- Track deaths for stats
  37.             Obby.HandlePlayerDeath(player, character)
  38.         end)
  39.     end)
  40. end
  41.  
  42. -- Creates and sets up leaderstats (Stage and Deaths)
  43. function Obby.CreateLeaderstats(player)
  44.     local leaderstats = Instance.new("Folder")
  45.     leaderstats.Name = "leaderstats"
  46.     leaderstats.Parent = player
  47.  
  48.     local stage = Instance.new("NumberValue", leaderstats)
  49.     stage.Name = "Stage"
  50.     stage.Value = 1 -- Everyone starts at stage 1
  51.  
  52.     local deaths = Instance.new("NumberValue", leaderstats)
  53.     deaths.Name = "Deaths"
  54.     deaths.Value = 0
  55. end
  56.  
  57. -- Teleports the player to the correct spawn based on their current stage
  58. function Obby.HandlePlayerRespawn(player)
  59.     local leaderstats = player:FindFirstChild("leaderstats")
  60.     if not leaderstats then return end
  61.  
  62.     local stageValue = leaderstats:FindFirstChild("Stage")
  63.     if not stageValue then return end
  64.  
  65.     local stage = stageValue.Value
  66.     local stageFolder = Workspace.Stages:FindFirstChild(tostring(stage))
  67.     if not stageFolder then return end
  68.  
  69.     local spawnLocation = stageFolder:FindFirstChild("SpawnLocation")
  70.     if not spawnLocation then return end
  71.  
  72.     -- Preload the area before teleporting the player
  73.     player:RequestStreamAroundAsync(spawnLocation.Position)
  74.  
  75.     local character = player.Character
  76.     if character then
  77.         character:PivotTo(spawnLocation.CFrame)
  78.     end
  79. end
  80.  
  81. -- When the player dies, increase their death count
  82. function Obby.HandlePlayerDeath(player, character)
  83.     local humanoid = character:FindFirstChildOfClass("Humanoid")
  84.     if humanoid then
  85.         humanoid.Died:Connect(function()
  86.             local leaderstats = player:FindFirstChild("leaderstats")
  87.             if leaderstats then
  88.                 local deaths = leaderstats:FindFirstChild("Deaths")
  89.                 if deaths then
  90.                     deaths.Value += 1
  91.                 end
  92.             end
  93.         end)
  94.     end
  95. end
  96.  
  97. -- Makes a portal teleport anything that touches it to a destination point
  98. function Obby.CreatePortal(portal, destination)
  99.     portal.Touched:Connect(function(other)
  100.         local char = other.Parent
  101.         local root = char and char:FindFirstChild("HumanoidRootPart")
  102.         if root then
  103.             -- Move the player a little above the destination so they don’t get stuck
  104.             root.CFrame = destination * CFrame.new(0, 3, 0)
  105.         end
  106.     end)
  107. end
  108.  
  109. -- Detects when players touch spawn locations and updates their checkpoint
  110. function Obby.CreateSpawnLocations()
  111.     for _, spawn in ipairs(Workspace.Stages:GetDescendants()) do
  112.         if spawn:IsA("SpawnLocation") then
  113.             spawn.Touched:Connect(function(otherPart)
  114.                 local character = otherPart.Parent
  115.                 if not character or not character:IsA("Model") then return end
  116.  
  117.                 local player = Players:GetPlayerFromCharacter(character)
  118.                 if not player then return end
  119.  
  120.                 -- Don't let the spawn trigger instantly after respawn
  121.                 local spawnTime = character:GetAttribute("SpawnTime")
  122.                 if spawnTime and tick() - spawnTime < 2 then return end
  123.  
  124.                 local leaderstats = player:FindFirstChild("leaderstats")
  125.                 if not leaderstats then return end
  126.  
  127.                 local stageValue = leaderstats:FindFirstChild("Stage")
  128.                 if not stageValue then return end
  129.  
  130.                 local newStage = tonumber(spawn.Parent.Name)
  131.                 if newStage and newStage > stageValue.Value then
  132.                     stageValue.Value = newStage
  133.                     Obby.ShowCheckpointUI(player)
  134.                 end
  135.             end)
  136.         end
  137.     end
  138. end
  139.  
  140. -- These parts kill the player when touched
  141. function Obby.CreateKillParts()
  142.     for _, part in ipairs(Workspace.Stages:GetDescendants()) do
  143.         if part:IsA("BasePart") and part.Name == "Kill" then
  144.             part.Touched:Connect(function(otherPart)
  145.                 local character = otherPart.Parent
  146.                 if character and character:IsA("Model") then
  147.                     local humanoid = character:FindFirstChildOfClass("Humanoid")
  148.                     if humanoid and humanoid.Health > 0 then
  149.                         humanoid.Health = 0 -- Instant death
  150.                     end
  151.                 end
  152.             end)
  153.         end
  154.     end
  155. end
  156.  
  157. -- Platforms that disappear temporarily after being touched
  158. function Obby.CreateFakePlatforms()
  159.     for _, part in ipairs(Workspace.Stages:GetDescendants()) do
  160.         if part:IsA("BasePart") and part.Name == "FakePlatform" then
  161.             part.Touched:Connect(function(otherPart)
  162.                 local character = otherPart.Parent
  163.                 if character and character:IsA("Model") then
  164.                     task.wait(0.0) -- Let them land first
  165.                     part.Transparency = 1
  166.                     part.CanCollide = false
  167.                     task.wait(3) -- Bring it back after 3 seconds
  168.                     part.Transparency = 0
  169.                     part.CanCollide = true
  170.                 end
  171.             end)
  172.         end
  173.     end
  174. end
  175.  
  176. -- Moves platforms back and forth using TweenService
  177. function Obby.CreateMovingPlatforms()
  178.     for _, platform in ipairs(Workspace.Stages:GetDescendants()) do
  179.         if platform:IsA("BasePart") and platform.Name == "MovingPlatform" then
  180.             local targetPosition = platform:GetAttribute("TargetPosition")
  181.             local moveTime = platform:GetAttribute("MoveTime") or 3
  182.             local waitTime = platform:GetAttribute("WaitTime") or 1
  183.  
  184.             if targetPosition then
  185.                 local startPos = platform.Position
  186.                 local endPos = Vector3.new(targetPosition.X, targetPosition.Y, targetPosition.Z)
  187.  
  188.                 local tweenInfo = TweenInfo.new(
  189.                     moveTime,
  190.                     Enum.EasingStyle.Sine,
  191.                     Enum.EasingDirection.InOut,
  192.                     -1, -- Infinite loops
  193.                     true -- Ping-pong back and forth
  194.                 )
  195.  
  196.                 local tween = TweenService:Create(platform, tweenInfo, {Position = endPos})
  197.                 tween:Play()
  198.  
  199.                 -- This keeps the platform's velocity synced so characters ride it smoothly
  200.                 local lastPosition = platform.Position
  201.  
  202.                 RunService.Stepped:Connect(function(_, deltatime)
  203.                     local currentPosition = platform.Position
  204.                     local deltaPosition = currentPosition - lastPosition
  205.                     local velocity = deltaPosition / deltatime
  206.  
  207.                     platform.AssemblyLinearVelocity = velocity
  208.                     lastPosition = currentPosition
  209.                 end)
  210.             end
  211.         end
  212.     end
  213. end
  214.  
  215. -- Handles a level where players choose between deadly or safe doors
  216. function Obby.CreateChoiceDoors()
  217.     local stages = Workspace:FindFirstChild("Stages")
  218.     if not stages then return end
  219.  
  220.     local choiceDoors = stages:FindFirstChild("20") -- Only runs if stage 20 exists
  221.     if not choiceDoors then return end
  222.  
  223.     for _, doorModel in ipairs(choiceDoors:GetChildren()) do
  224.         if doorModel:IsA("Model") then
  225.             local trapDoor = doorModel:FindFirstChild("TrapDoor")
  226.             local rightDoor = doorModel:FindFirstChild("RightDoor")
  227.  
  228.             -- Trap door kills you if you touch it
  229.             if trapDoor and trapDoor:IsA("BasePart") then
  230.                 trapDoor.Touched:Connect(function(otherPart)
  231.                     local character = otherPart.Parent
  232.                     local humanoid = character and character:FindFirstChildOfClass("Humanoid")
  233.                     if humanoid and humanoid.Health > 0 then
  234.                         humanoid.Health = 0
  235.                     end
  236.                 end)
  237.             end
  238.  
  239.             -- Right door lets you through temporarily
  240.             if rightDoor and rightDoor:IsA("BasePart") then
  241.                 rightDoor.Touched:Connect(function(otherPart)
  242.                     local character = otherPart.Parent
  243.                     local humanoid = character and character:FindFirstChildOfClass("Humanoid")
  244.                     if humanoid then
  245.                         rightDoor.CanCollide = false
  246.                         rightDoor.Transparency = 0.5
  247.                         task.wait(2)
  248.                         rightDoor.CanCollide = true
  249.                         rightDoor.Transparency = 0.35
  250.                     end
  251.                 end)
  252.             end
  253.         end
  254.     end
  255. end
  256.  
  257. -- Shows a little UI popup when a checkpoint is reached
  258. function Obby.ShowCheckpointUI(player)
  259.     local playerGui = player:FindFirstChildOfClass("PlayerGui")
  260.     if not playerGui then return end
  261.  
  262.     local existingUI = playerGui:FindFirstChild("CheckpointUI")
  263.     local ui
  264.  
  265.     if existingUI then
  266.         ui = existingUI
  267.     else
  268.         local template = ReplicatedStorage:FindFirstChild("CheckpointUI")
  269.         if not template then return end
  270.         ui = template:Clone()
  271.         ui.Parent = playerGui
  272.     end
  273.  
  274.     local label = ui:FindFirstChildOfClass("TextLabel")
  275.     if not label then return end
  276.  
  277.     label.Visible = true
  278.     label.TextTransparency = 1
  279.  
  280.     -- Fade in the text
  281.     for i = 0, 1, 0.1 do
  282.         label.TextTransparency = 1 - i
  283.         task.wait(0.03)
  284.     end
  285.  
  286.     task.wait(0.4)
  287.  
  288.     -- Fade it back out
  289.     for i = 0, 1, 0.1 do
  290.         label.TextTransparency = i
  291.         task.wait(0.03)
  292.     end
  293.  
  294.     label.Visible = false
  295. end
  296.  
  297. -- return the module
  298. return Obby
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement