Advertisement
MysteriaFool

Untitled

Apr 23rd, 2025 (edited)
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.77 KB | None | 0 0
  1. -- Get the Services.
  2. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  3. local Players = game:GetService("Players")
  4. local Workspace = game:GetService("Workspace")
  5. local CollectionService = game:GetService("CollectionService")
  6. local RunService = game:GetService("RunService")
  7. local TweenService = game:GetService("TweenService")
  8.  
  9. -- Obby Module.
  10. local Obby = {}
  11.  
  12. -- a small function to initialize the game and start all required processes.
  13. function Obby.OnStart()
  14.     Obby.CreateKillParts() -- Creates the kill parts.
  15.     Obby.CreateSpawnLocations() -- Sets up the spawn locations.
  16.     Obby.CreateFakePlatforms() -- Sets up fake platforms.
  17.     Obby.CreateMovingPlatforms() -- Sets up moving platforms.
  18.     Obby.CreateColorChangingPlatforms() -- Sets up Color-Changing platforms.
  19.  
  20.     Players.PlayerAdded:Connect(function(player: Player) -- When a new player joins the game.
  21.         if not player:FindFirstChild("leaderstats") then -- If the player doesn't have leaderstats.
  22.             Obby.CreateLeaderstats(player) -- create it.
  23.         end
  24.  
  25.  
  26.         player.CharacterAdded:Connect(function(character) -- When the player's character is added.
  27.             character:SetAttribute("SpawnTime", tick()) -- sometimes players get assigned higher stages when they first spawn into the game. this is to prevent that,
  28.             Obby.HandlePlayerRespawn(player) -- Sets up the Respawn.
  29.             Obby.HandlePlayerDeath(player, character) -- Sets up the Death.
  30.         end)
  31.     end)
  32. end
  33.  
  34. -- Handles the Leaderstats For The Obby.
  35. function Obby.CreateLeaderstats(player: Player)
  36.     local leaderstats = Instance.new("Folder") -- Folder to hold player stats.
  37.     leaderstats.Name = "leaderstats" -- folder name is "leaderstats".
  38.     leaderstats.Parent = player -- folder's parent is "player".
  39.  
  40.     local stage = Instance.new("NumberValue") -- create a numbervalue inside the folder.
  41.     stage.Name = "Stage" -- numbervalue's name is "Stage"
  42.     stage.Value = 1 -- Tracks what stage the player is at.
  43.     stage.Parent = leaderstats -- parent it to leaderstats
  44.  
  45.     local deaths = Instance.new("NumberValue") -- create a numbervalue inside the folder.
  46.     deaths.Name = "Deaths" -- numbervalue's name is "Deaths"
  47.     deaths.Value = 0 -- Tracks how many times the player has died.
  48.     deaths.Parent = leaderstats -- parent it to leaderstats.
  49. end
  50.  
  51. -- Handles player's respawn and Stage updates.
  52. function Obby.HandlePlayerRespawn(player: Player)
  53.     local leaderstats = player:FindFirstChild("leaderstats") -- Get the leaderstats folder for the player.
  54.     if not leaderstats then return end -- end if no leaderstats.
  55.  
  56.     local Stages = leaderstats:FindFirstChild("Stage") -- Get the Stage from leaderstats.
  57.     if not Stages then return end -- end if no Stages.
  58.  
  59.     local stage = Stages.Value -- Store the player's current stage value.
  60.  
  61.     for _, spawn in ipairs(CollectionService:GetTagged("CheckpointSpawn")) do -- Loop through all spawn points tagged with "CheckpointSpawn".
  62.         if spawn:IsA("SpawnLocation") and spawn:GetAttribute("Stage") == stage then -- Check if the spawn is a valid "SpawnLocation" and matches the player's current stage.
  63.  
  64.             local character = player.Character -- Get the player's character.
  65.             if character then -- Check if the character exists.
  66.                 character:PivotTo(spawn.CFrame) -- Respawn the player at the spawn point.
  67.             end
  68.  
  69.             break -- Exit the loop after respawning the player at the spawn point.
  70.         end
  71.     end
  72.     --[[
  73.     HandlePlayerRespawn System:
  74.     - Tag: "CheckpointSpawn"
  75.  
  76.     - Attribute: "Stage" (number) - Represents the stage the player is at.
  77.    
  78.     - Behavior: Respawns the player at the correct checkpoint based on their current stage.
  79.     --]]
  80. end
  81.  
  82. -- Handles the player's death.
  83. function Obby.HandlePlayerDeath(player: Player, character: Model)
  84.     local deathMessages = { -- list of messages that can appear when the player dies.
  85.     "You Died.",
  86.     "Better luck next time.",
  87.     "That's unlucky.",
  88.     "Oops!",
  89.     "Try again!",
  90.     "Not like this...",
  91.     "Close one!",
  92.     "One more time!",
  93. }
  94.     local humanoid = character:FindFirstChildOfClass("Humanoid") -- Get the player's humanoid object.
  95.     if not humanoid then return end -- end if no humanoid.
  96.  
  97.     humanoid.Died:Connect(function() -- Triggered when the player's humanoid dies.
  98.         local leaderstats = player:FindFirstChild("leaderstats") -- Find the player's leaderstats folder.
  99.         if not leaderstats then return end -- end if no leaderstats.
  100.        
  101.         local deaths = leaderstats:FindFirstChild("Deaths") -- Find the "Deaths" stat in leaderstats.
  102.         if deaths then -- If the "Deaths" stat exists.
  103.             deaths.Value += 1 -- Increase the death count by 1.
  104.         end
  105.  
  106.         Obby.ShowUIMessage(player, "DeathUI", deathMessages) -- Display UI After Player Dies.
  107.     end)
  108.     --[[
  109.     HandlePlayerDeath System:
  110.     - Event: "Died" (triggered when the humanoid dies)
  111.  
  112.     - Attribute: "Deaths" (number) - Tracks the player's death count.
  113.  
  114.     - Behavior: Increases the death count each time the player dies.
  115.     --]]
  116. end
  117.  
  118. -- Handles Teleporting the player to other Places.
  119. function Obby.CreatePortal(portal: BasePart, destination: BasePart)
  120.     portal.Touched:Connect(function(other) -- Triggered when something touches the portal.
  121.         local char = other.Parent -- Get the character.
  122.         if not char:FindFirstChildOfClass("Humanoid") then return end -- end if it's not a player character.
  123.  
  124.         local root = char:FindFirstChild("HumanoidRootPart") -- Get the player's root.
  125.         if root then -- if root found.
  126.             root.CFrame = destination * CFrame.new(0, 3, 0) -- Teleport the player to the destination, offset slightly upward to prevent getting stuck.
  127.         end
  128.     end)
  129. end
  130.  
  131. -- Handles The Spawnlocations and The player's Progression.
  132. function Obby.CreateSpawnLocations()
  133.     local spawns = CollectionService:GetTagged("CheckpointSpawn") -- Get all parts tagged as checkpoint spawns.
  134.  
  135.     for _, spawn in ipairs(spawns) do -- Loop through each checkpoint part.
  136.         spawn.Touched:Connect(function(otherPart: BasePart) -- Runs when a player touches the checkpoint.
  137.             local character = otherPart.Parent -- Get the player's character.
  138.             if not character or not character:IsA("Model") then return end -- end if no character.
  139.  
  140.             local player = Players:GetPlayerFromCharacter(character) -- Get the Player instance from the character.
  141.             if not player then return end -- end if no player.
  142.  
  143.             local spawnTime = character:GetAttribute("SpawnTime") -- Cooldown to prevent triggering checkpoint too early after respawn.
  144.             if spawnTime and tick() - spawnTime < 2 then return end -- end if cooldown hasn't passed.
  145.  
  146.             local leaderstats = player:FindFirstChild("leaderstats") -- Access the player's leaderstats folder.
  147.             if not leaderstats then return end -- end if no leaderstats.
  148.  
  149.             local stageValue = leaderstats:FindFirstChild("Stage") -- Get the current stage number so we can update it.
  150.             if not stageValue then return end -- end if no stagevalue found.
  151.  
  152.             local newStage = tonumber(spawn:GetAttribute("Stage")) -- Get the checkpoint's current stage from the "Stage" Attribute NumberValue.
  153.             if newStage and newStage > stageValue.Value then -- Only update the stage if it's ahead of the player's current stage.
  154.                 stageValue.Value = newStage -- Save progress to the new stage.
  155.                 Obby.ShowUIMessage(player, "CheckpointUI")-- Display checkpoint reached UI to the player.
  156.             end
  157.         end)
  158.     end
  159.     --[[
  160.     Checkpoint System:
  161.     - Tag: "CheckpointSpawn"
  162.    
  163.     - Attributes:
  164.         "Stage" (number) - The stage number associated with the checkpoint.
  165.    
  166.     - Behavior:
  167.         - When a player touches the checkpoint, the player's stage progress is updated.
  168.         - Displays UI for the player indicating the checkpoint has been reached.
  169.     --]]
  170. end
  171.  
  172. -- Handles Parts that damage players on touch.
  173. function Obby.CreateKillParts()
  174.     local killParts = CollectionService:GetTagged("Kill") -- Get all parts tagged with "Kill".
  175.    
  176.     for _, part in ipairs(killParts) do -- Loop through each part tagged as "Kill".
  177.  
  178.         part.Touched:Connect(function(otherPart: BasePart) -- Triggered when a player touches a "Kill" part.
  179.             local character = otherPart.Parent -- Get the character model from the touched part.
  180.             if character and character:IsA("Model") then -- If the touched part belongs to a character.
  181.                 local humanoid = character:FindFirstChildOfClass("Humanoid") -- Look for the humanoid within the character.
  182.                 if humanoid and humanoid.Health > 0 then -- Check if the humanoid exists and is alive.
  183.                     humanoid:TakeDamage(100) -- Apply 100 damage to the player.
  184.                 end
  185.             end
  186.         end)
  187.     --[[
  188.     KillPart System:
  189.     - Tag: "Kill"
  190.  
  191.     - Behavior: Instantly damages the player (100 HP) on touch.
  192.     --]]
  193.     end
  194. end
  195.  
  196. -- Handles Parts that disappear on player's touch.
  197. function Obby.CreateFakePlatforms()
  198.     local fakePlatforms = CollectionService:GetTagged("FakePlatform") -- Get all parts tagged with "FakePlatForm".
  199.  
  200.     for _, part in ipairs(fakePlatforms) do -- Loop through each fakePlatforms part.
  201.  
  202.             part.Touched:Connect(function(otherPart) -- Triggered when a player touches a "FakePlatForm" part.
  203.  
  204.                 if part:GetAttribute("IsFalling") then return end -- Debounce per platform to prevent it from triggering multiple times.
  205.  
  206.                 local character = otherPart.Parent -- get the character.
  207.                 if not character or not character:IsA("Model") then return end -- end if character not found.
  208.  
  209.                 local humanoid = character:FindFirstChildOfClass("Humanoid") -- find humanoid.
  210.                 if not humanoid or humanoid.Health <= 0 then return end -- end if humanoid not found.
  211.  
  212.                 part:SetAttribute("IsFalling", true) -- Set a temporary flag.
  213.  
  214.                 part.Transparency = 1 -- Make the platform invisible.
  215.                 part.CanCollide = false -- Disable collisions so players fall through.
  216.  
  217.                 local resetTime = part:GetAttribute("ResetTime") or 3 -- Get The ResetTime Attribute or Use Default 3 seconds timer.
  218.                 task.wait(resetTime) -- Wait
  219.  
  220.                 part.Transparency = 0.35 -- Make the platform visible again.
  221.                 part.CanCollide = true -- Re-enable collisions.
  222.  
  223.                 part:SetAttribute("IsFalling", false) -- Allow it to trigger again.
  224.             end)
  225.         end
  226.         --[[
  227.          FakePlatform System:
  228.          - Tag: "FakePlatform"
  229.  
  230.          - Attribute:
  231.          "ResetTime" (number, seconds) - How long the platform stays gone.
  232.  
  233.          - Behavior: Disappears on player touch, reappears after reset.
  234.         --]]
  235. end
  236.  
  237. -- Handles Parts That move between two points using tweens.
  238. function Obby.CreateMovingPlatforms()
  239.     local movingPlatforms = CollectionService:GetTagged("MovingPlatform") -- Get all parts tagged with "MovingPlatform".
  240.  
  241.     for _, platform in ipairs(movingPlatforms) do -- Loop through each movingPlatforms part.
  242.  
  243.         local targetPosition = platform:GetAttribute("TargetPosition") -- Get the target position attribute. This is where the platform will tween to.
  244.         if not targetPosition then return end -- end if no targetPosition.
  245.  
  246.         local moveTime = platform:GetAttribute("MoveTime") or 3 -- How long the platform takes to reach the target position.
  247.  
  248.         local endPos = Vector3.new(targetPosition.X, targetPosition.Y, targetPosition.Z) -- Convert the target position into a Vector3.
  249.         local tweenInfo = TweenInfo.new(moveTime, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut, -1, true) -- Create a TweenInfo with infinite reversals
  250.         local tween = TweenService:Create(platform, tweenInfo, {Position = endPos}) -- Create the tween to move the platform between its current and target positions.
  251.         tween:Play() -- Play the Tween
  252.     end
  253.     --[[
  254.     MovingPlatform System:
  255.     - Tag: "MovingPlatform"
  256.  
  257.     - Attributes:
  258.         "TargetPosition" (Vector3) - The destination position for the platform.
  259.         "MoveTime" (number, seconds) - Duration for the platform to complete its move.
  260.  
  261.     - Behavior:
  262.         - Tweens the platform between its current position and the target position.
  263.         - The platform moves back and forth infinitely with Eeasing style and direction.
  264.     --]]
  265. end
  266.  
  267. -- Handles Color Changing Platforms that kill the player when red.
  268. function Obby.CreateColorChangingPlatforms()
  269.     local colorChangingPlatforms = CollectionService:GetTagged("ColorChanging") -- Get all parts tagged with "ColorChanging".
  270.  
  271.     for _, platform in ipairs(colorChangingPlatforms) do -- Loop through each platform tagged as "ColorChanging".
  272.  
  273.         local originalColor = platform.Color -- Store the original color of the platform.
  274.  
  275.         local function ChangeColor() -- Function to periodically change the platform color to red and back to the original color.
  276.             while true do
  277.                 platform.Color = Color3.fromRGB(255, 0, 0) -- Change platform color to red.
  278.                 task.wait(2) -- Wait for 2 seconds before switching back to original color.
  279.  
  280.                 platform.Color = originalColor -- Change platform color back to original.
  281.                 task.wait(2) -- Wait for 2 seconds before changing again.
  282.             end
  283.         end
  284.  
  285.         task.spawn(ChangeColor) -- Start the color changing process in a separate thread.
  286.  
  287.         platform.Touched:Connect(function(otherPart) -- Triggered when a player touches the platform.
  288.             local character = otherPart.Parent -- Get the character model from the touched part.
  289.             if not character or not character:IsA("Model") then return end -- end if no character.
  290.  
  291.             local humanoid = character:FindFirstChildOfClass("Humanoid") -- Look for the humanoid within the character.
  292.             if not humanoid or humanoid.Health <= 0 then return end -- end if no humanoid.
  293.  
  294.  
  295.             if platform.Color == Color3.fromRGB(255, 0, 0) then -- If the platform is red, then
  296.                 humanoid:TakeDamage(100) -- Apply damage to the player (100 HP).
  297.             end
  298.         end)
  299.     end
  300.     --[[
  301.     ColorChangingPlatform System:
  302.     - Tag: "ColorChanging"
  303.  
  304.     - Behavior:
  305.     - The platform changes color periodically between red and back to its original color every 2 seconds.
  306.     - If the platform turns red and a player touches it, the player takes damage.
  307.     - The platform color is changed in a continuous loop in the background.
  308.     --]]
  309. end
  310.  
  311. -- Handles displaying UI messages to the player.
  312. function Obby.ShowUIMessage(player, guiName, messages)
  313.     local playerGui = player:FindFirstChildOfClass("PlayerGui") -- Access the PlayerGui of the player.
  314.     if not playerGui then return end -- end if no playergui.
  315.  
  316.     local ui = playerGui:FindFirstChild(guiName) or (ReplicatedStorage:FindFirstChild(guiName) and ReplicatedStorage[guiName]:Clone()) -- Clone the UI from ReplicatedStorage if it's not already in PlayerGui
  317.     if not ui then return end -- end if no UI.
  318.  
  319.     ui.Parent = playerGui -- Parent the UI to the player's PlayerGui.
  320.  
  321.     local label = ui:FindFirstChildOfClass("TextLabel") -- Find the TextLabel inside the UI.
  322.     if not label then return end -- End if no TextLabel
  323.  
  324.     if messages and #messages > 0 then -- If messages are provided and not empty,
  325.         label.Text = messages[math.random(1, #messages)] -- Randomly pick one and set it as the label's text.
  326.     end
  327.  
  328.     label.Visible = true -- Make the label visible.
  329.     label.TextTransparency = 1 -- Start fully transparent.
  330.     for i = 0, 1, 0.1 do -- Gradually make text visible.
  331.         label.TextTransparency = 1 - i
  332.         task.wait(0.03)
  333.     end
  334.  
  335.     task.wait(1.5) -- Wait and Display the message briefly.
  336.  
  337.     -- UI Fade Out
  338.     for i = 0, 1, 0.1 do -- Gradually make text invisible.
  339.         label.TextTransparency = i
  340.         task.wait(0.03)
  341.     end
  342.  
  343.     label.Visible = false -- Hide the label.
  344.     --[[
  345.     ShowUIMessage System:
  346.  
  347.     - Parameters:
  348.     - player (Player): The player to whom the message will be shown.
  349.     - guiName (string): The name of the GUI object located in ReplicatedStorage.
  350.     - messages (table of strings) [optional]: A list of messages to randomly choose from.
  351.  
  352.     - Behavior:
  353.     - Clones a GUI from ReplicatedStorage into the Player's PlayerGui if not already present.
  354.     - Displays a message by fading the TextLabel in and out.
  355.     --]]
  356. end
  357.  
  358. return Obby
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement