Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Get the Services.
- local ReplicatedStorage = game:GetService("ReplicatedStorage")
- local Players = game:GetService("Players")
- local Workspace = game:GetService("Workspace")
- local CollectionService = game:GetService("CollectionService")
- local RunService = game:GetService("RunService")
- local TweenService = game:GetService("TweenService")
- -- Obby Module.
- local Obby = {}
- -- a small function to initialize the game and start all required processes.
- function Obby.OnStart()
- Obby.CreateKillParts() -- Creates the kill parts.
- Obby.CreateSpawnLocations() -- Sets up the spawn locations.
- Obby.CreateFakePlatforms() -- Sets up fake platforms.
- Obby.CreateMovingPlatforms() -- Sets up moving platforms.
- Obby.CreateColorChangingPlatforms() -- Sets up Color-Changing platforms.
- Players.PlayerAdded:Connect(function(player: Player) -- When a new player joins the game.
- if not player:FindFirstChild("leaderstats") then -- If the player doesn't have leaderstats.
- Obby.CreateLeaderstats(player) -- create it.
- end
- player.CharacterAdded:Connect(function(character) -- When the player's character is added.
- character:SetAttribute("SpawnTime", tick()) -- sometimes players get assigned higher stages when they first spawn into the game. this is to prevent that,
- Obby.HandlePlayerRespawn(player) -- Sets up the Respawn.
- Obby.HandlePlayerDeath(player, character) -- Sets up the Death.
- end)
- end)
- end
- -- Handles the Leaderstats For The Obby.
- function Obby.CreateLeaderstats(player: Player)
- local leaderstats = Instance.new("Folder") -- Folder to hold player stats.
- leaderstats.Name = "leaderstats" -- folder name is "leaderstats".
- leaderstats.Parent = player -- folder's parent is "player".
- local stage = Instance.new("NumberValue") -- create a numbervalue inside the folder.
- stage.Name = "Stage" -- numbervalue's name is "Stage"
- stage.Value = 1 -- Tracks what stage the player is at.
- stage.Parent = leaderstats -- parent it to leaderstats
- local deaths = Instance.new("NumberValue") -- create a numbervalue inside the folder.
- deaths.Name = "Deaths" -- numbervalue's name is "Deaths"
- deaths.Value = 0 -- Tracks how many times the player has died.
- deaths.Parent = leaderstats -- parent it to leaderstats.
- end
- -- Handles player's respawn and Stage updates.
- function Obby.HandlePlayerRespawn(player: Player)
- local leaderstats = player:FindFirstChild("leaderstats") -- Get the leaderstats folder for the player.
- if not leaderstats then return end -- end if no leaderstats.
- local Stages = leaderstats:FindFirstChild("Stage") -- Get the Stage from leaderstats.
- if not Stages then return end -- end if no Stages.
- local stage = Stages.Value -- Store the player's current stage value.
- for _, spawn in ipairs(CollectionService:GetTagged("CheckpointSpawn")) do -- Loop through all spawn points tagged with "CheckpointSpawn".
- 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.
- local character = player.Character -- Get the player's character.
- if character then -- Check if the character exists.
- character:PivotTo(spawn.CFrame) -- Respawn the player at the spawn point.
- end
- break -- Exit the loop after respawning the player at the spawn point.
- end
- end
- --[[
- HandlePlayerRespawn System:
- - Tag: "CheckpointSpawn"
- - Attribute: "Stage" (number) - Represents the stage the player is at.
- - Behavior: Respawns the player at the correct checkpoint based on their current stage.
- --]]
- end
- -- Handles the player's death.
- function Obby.HandlePlayerDeath(player: Player, character: Model)
- local deathMessages = { -- list of messages that can appear when the player dies.
- "You Died.",
- "Better luck next time.",
- "That's unlucky.",
- "Oops!",
- "Try again!",
- "Not like this...",
- "Close one!",
- "One more time!",
- }
- local humanoid = character:FindFirstChildOfClass("Humanoid") -- Get the player's humanoid object.
- if not humanoid then return end -- end if no humanoid.
- humanoid.Died:Connect(function() -- Triggered when the player's humanoid dies.
- local leaderstats = player:FindFirstChild("leaderstats") -- Find the player's leaderstats folder.
- if not leaderstats then return end -- end if no leaderstats.
- local deaths = leaderstats:FindFirstChild("Deaths") -- Find the "Deaths" stat in leaderstats.
- if deaths then -- If the "Deaths" stat exists.
- deaths.Value += 1 -- Increase the death count by 1.
- end
- Obby.ShowUIMessage(player, "DeathUI", deathMessages) -- Display UI After Player Dies.
- end)
- --[[
- HandlePlayerDeath System:
- - Event: "Died" (triggered when the humanoid dies)
- - Attribute: "Deaths" (number) - Tracks the player's death count.
- - Behavior: Increases the death count each time the player dies.
- --]]
- end
- -- Handles Teleporting the player to other Places.
- function Obby.CreatePortal(portal: BasePart, destination: BasePart)
- portal.Touched:Connect(function(other) -- Triggered when something touches the portal.
- local char = other.Parent -- Get the character.
- if not char:FindFirstChildOfClass("Humanoid") then return end -- end if it's not a player character.
- local root = char:FindFirstChild("HumanoidRootPart") -- Get the player's root.
- if root then -- if root found.
- root.CFrame = destination * CFrame.new(0, 3, 0) -- Teleport the player to the destination, offset slightly upward to prevent getting stuck.
- end
- end)
- end
- -- Handles The Spawnlocations and The player's Progression.
- function Obby.CreateSpawnLocations()
- local spawns = CollectionService:GetTagged("CheckpointSpawn") -- Get all parts tagged as checkpoint spawns.
- for _, spawn in ipairs(spawns) do -- Loop through each checkpoint part.
- spawn.Touched:Connect(function(otherPart: BasePart) -- Runs when a player touches the checkpoint.
- local character = otherPart.Parent -- Get the player's character.
- if not character or not character:IsA("Model") then return end -- end if no character.
- local player = Players:GetPlayerFromCharacter(character) -- Get the Player instance from the character.
- if not player then return end -- end if no player.
- local spawnTime = character:GetAttribute("SpawnTime") -- Cooldown to prevent triggering checkpoint too early after respawn.
- if spawnTime and tick() - spawnTime < 2 then return end -- end if cooldown hasn't passed.
- local leaderstats = player:FindFirstChild("leaderstats") -- Access the player's leaderstats folder.
- if not leaderstats then return end -- end if no leaderstats.
- local stageValue = leaderstats:FindFirstChild("Stage") -- Get the current stage number so we can update it.
- if not stageValue then return end -- end if no stagevalue found.
- local newStage = tonumber(spawn:GetAttribute("Stage")) -- Get the checkpoint's current stage from the "Stage" Attribute NumberValue.
- if newStage and newStage > stageValue.Value then -- Only update the stage if it's ahead of the player's current stage.
- stageValue.Value = newStage -- Save progress to the new stage.
- Obby.ShowUIMessage(player, "CheckpointUI")-- Display checkpoint reached UI to the player.
- end
- end)
- end
- --[[
- Checkpoint System:
- - Tag: "CheckpointSpawn"
- - Attributes:
- "Stage" (number) - The stage number associated with the checkpoint.
- - Behavior:
- - When a player touches the checkpoint, the player's stage progress is updated.
- - Displays UI for the player indicating the checkpoint has been reached.
- --]]
- end
- -- Handles Parts that damage players on touch.
- function Obby.CreateKillParts()
- local killParts = CollectionService:GetTagged("Kill") -- Get all parts tagged with "Kill".
- for _, part in ipairs(killParts) do -- Loop through each part tagged as "Kill".
- part.Touched:Connect(function(otherPart: BasePart) -- Triggered when a player touches a "Kill" part.
- local character = otherPart.Parent -- Get the character model from the touched part.
- if character and character:IsA("Model") then -- If the touched part belongs to a character.
- local humanoid = character:FindFirstChildOfClass("Humanoid") -- Look for the humanoid within the character.
- if humanoid and humanoid.Health > 0 then -- Check if the humanoid exists and is alive.
- humanoid:TakeDamage(100) -- Apply 100 damage to the player.
- end
- end
- end)
- --[[
- KillPart System:
- - Tag: "Kill"
- - Behavior: Instantly damages the player (100 HP) on touch.
- --]]
- end
- end
- -- Handles Parts that disappear on player's touch.
- function Obby.CreateFakePlatforms()
- local fakePlatforms = CollectionService:GetTagged("FakePlatform") -- Get all parts tagged with "FakePlatForm".
- for _, part in ipairs(fakePlatforms) do -- Loop through each fakePlatforms part.
- part.Touched:Connect(function(otherPart) -- Triggered when a player touches a "FakePlatForm" part.
- if part:GetAttribute("IsFalling") then return end -- Debounce per platform to prevent it from triggering multiple times.
- local character = otherPart.Parent -- get the character.
- if not character or not character:IsA("Model") then return end -- end if character not found.
- local humanoid = character:FindFirstChildOfClass("Humanoid") -- find humanoid.
- if not humanoid or humanoid.Health <= 0 then return end -- end if humanoid not found.
- part:SetAttribute("IsFalling", true) -- Set a temporary flag.
- part.Transparency = 1 -- Make the platform invisible.
- part.CanCollide = false -- Disable collisions so players fall through.
- local resetTime = part:GetAttribute("ResetTime") or 3 -- Get The ResetTime Attribute or Use Default 3 seconds timer.
- task.wait(resetTime) -- Wait
- part.Transparency = 0.35 -- Make the platform visible again.
- part.CanCollide = true -- Re-enable collisions.
- part:SetAttribute("IsFalling", false) -- Allow it to trigger again.
- end)
- end
- --[[
- FakePlatform System:
- - Tag: "FakePlatform"
- - Attribute:
- "ResetTime" (number, seconds) - How long the platform stays gone.
- - Behavior: Disappears on player touch, reappears after reset.
- --]]
- end
- -- Handles Parts That move between two points using tweens.
- function Obby.CreateMovingPlatforms()
- local movingPlatforms = CollectionService:GetTagged("MovingPlatform") -- Get all parts tagged with "MovingPlatform".
- for _, platform in ipairs(movingPlatforms) do -- Loop through each movingPlatforms part.
- local targetPosition = platform:GetAttribute("TargetPosition") -- Get the target position attribute. This is where the platform will tween to.
- if not targetPosition then return end -- end if no targetPosition.
- local moveTime = platform:GetAttribute("MoveTime") or 3 -- How long the platform takes to reach the target position.
- local endPos = Vector3.new(targetPosition.X, targetPosition.Y, targetPosition.Z) -- Convert the target position into a Vector3.
- local tweenInfo = TweenInfo.new(moveTime, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut, -1, true) -- Create a TweenInfo with infinite reversals
- local tween = TweenService:Create(platform, tweenInfo, {Position = endPos}) -- Create the tween to move the platform between its current and target positions.
- tween:Play() -- Play the Tween
- end
- --[[
- MovingPlatform System:
- - Tag: "MovingPlatform"
- - Attributes:
- "TargetPosition" (Vector3) - The destination position for the platform.
- "MoveTime" (number, seconds) - Duration for the platform to complete its move.
- - Behavior:
- - Tweens the platform between its current position and the target position.
- - The platform moves back and forth infinitely with Eeasing style and direction.
- --]]
- end
- -- Handles Color Changing Platforms that kill the player when red.
- function Obby.CreateColorChangingPlatforms()
- local colorChangingPlatforms = CollectionService:GetTagged("ColorChanging") -- Get all parts tagged with "ColorChanging".
- for _, platform in ipairs(colorChangingPlatforms) do -- Loop through each platform tagged as "ColorChanging".
- local originalColor = platform.Color -- Store the original color of the platform.
- local function ChangeColor() -- Function to periodically change the platform color to red and back to the original color.
- while true do
- platform.Color = Color3.fromRGB(255, 0, 0) -- Change platform color to red.
- task.wait(2) -- Wait for 2 seconds before switching back to original color.
- platform.Color = originalColor -- Change platform color back to original.
- task.wait(2) -- Wait for 2 seconds before changing again.
- end
- end
- task.spawn(ChangeColor) -- Start the color changing process in a separate thread.
- platform.Touched:Connect(function(otherPart) -- Triggered when a player touches the platform.
- local character = otherPart.Parent -- Get the character model from the touched part.
- if not character or not character:IsA("Model") then return end -- end if no character.
- local humanoid = character:FindFirstChildOfClass("Humanoid") -- Look for the humanoid within the character.
- if not humanoid or humanoid.Health <= 0 then return end -- end if no humanoid.
- if platform.Color == Color3.fromRGB(255, 0, 0) then -- If the platform is red, then
- humanoid:TakeDamage(100) -- Apply damage to the player (100 HP).
- end
- end)
- end
- --[[
- ColorChangingPlatform System:
- - Tag: "ColorChanging"
- - Behavior:
- - The platform changes color periodically between red and back to its original color every 2 seconds.
- - If the platform turns red and a player touches it, the player takes damage.
- - The platform color is changed in a continuous loop in the background.
- --]]
- end
- -- Handles displaying UI messages to the player.
- function Obby.ShowUIMessage(player, guiName, messages)
- local playerGui = player:FindFirstChildOfClass("PlayerGui") -- Access the PlayerGui of the player.
- if not playerGui then return end -- end if no playergui.
- 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
- if not ui then return end -- end if no UI.
- ui.Parent = playerGui -- Parent the UI to the player's PlayerGui.
- local label = ui:FindFirstChildOfClass("TextLabel") -- Find the TextLabel inside the UI.
- if not label then return end -- End if no TextLabel
- if messages and #messages > 0 then -- If messages are provided and not empty,
- label.Text = messages[math.random(1, #messages)] -- Randomly pick one and set it as the label's text.
- end
- label.Visible = true -- Make the label visible.
- label.TextTransparency = 1 -- Start fully transparent.
- for i = 0, 1, 0.1 do -- Gradually make text visible.
- label.TextTransparency = 1 - i
- task.wait(0.03)
- end
- task.wait(1.5) -- Wait and Display the message briefly.
- -- UI Fade Out
- for i = 0, 1, 0.1 do -- Gradually make text invisible.
- label.TextTransparency = i
- task.wait(0.03)
- end
- label.Visible = false -- Hide the label.
- --[[
- ShowUIMessage System:
- - Parameters:
- - player (Player): The player to whom the message will be shown.
- - guiName (string): The name of the GUI object located in ReplicatedStorage.
- - messages (table of strings) [optional]: A list of messages to randomly choose from.
- - Behavior:
- - Clones a GUI from ReplicatedStorage into the Player's PlayerGui if not already present.
- - Displays a message by fading the TextLabel in and out.
- --]]
- end
- return Obby
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement