Okay, scripting a full, polished Slendytubbies 3 campaign with complex AI, multiple maps, cutscenes, and a full UI is a massive undertaking that goes far beyond a single script.
However, I can provide a foundational framework in Roblox Lua. This framework will cover the core elements:
- Objective Tracking: Finding collectibles (custards).
- Basic Item Interaction: Picking up items.
- Simple Game State: Tracking collected items, winning/losing (conceptually).
- UI Update: Showing progress.
- Conceptual Monster Interaction: How a monster would interact with this system (without writing complex AI).
This requires multiple parts working together:
- A Server Script (in
ServerScriptService) to manage the game state and objectives.
- A Client Script (in
StarterGui) to display the UI.
- A Script inside each collectible item (in
Workspace) to handle interaction.
RemoteEvents (in ReplicatedStorage) for communication between client and server.
Let's break down the code for each part.
1. Setting up the Environment in Roblox Studio:
- Create a
Folder in ReplicatedStorage named Events.
- Inside the
Events folder, create two RemoteEvents:
CustardCollectedEvent
UpdateUIEvent
WinEvent
LoseEvent
- In
ServerScriptService, create a new Script named CampaignManager.
- In
StarterGui, create a ScreenGui named ObjectiveGui.
- Inside
ObjectiveGui, create a TextLabel named ObjectiveLabel. Position it somewhere visible.
- Inside
ObjectiveGui, create a LocalScript named ObjectiveScript.
- In
Workspace, create some Parts (these will be your custards). Name them something like CustardPart.
- Inside each
CustardPart, add a Script named CollectScript. Make sure CanCollide is true and Anchored is false (or handle collection differently if anchored).
2. Server Script (ServerScriptService/CampaignManager)
This script manages the overall game state, counts collected items, and checks for win/lose conditions.
-- // Services
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Workspace = game:GetService("Workspace")
-- // Events
local EventsFolder = ReplicatedStorage:WaitForChild("Events")
local CustardCollectedEvent = EventsFolder:WaitForChild("CustardCollectedEvent")
local UpdateUIEvent = EventsFolder:WaitForChild("UpdateUIEvent")
local WinEvent = EventsFolder:WaitForChild("WinEvent")
local LoseEvent = EventsFolder:WaitForChild("LoseEvent")
-- // Configuration
local TOTAL_CUSTARDS_NEEDED = 5 -- How many custards players need to find to win
-- // Game State (using a dictionary for multiple players)
local playersCollected = {} -- [Player.UserId] = count
-- // Function to update UI for a specific player
local function updatePlayerUI(player)
local collected = playersCollected[player.UserId] or 0
UpdateUIEvent:FireClient(player, collected, TOTAL_CUSTARDS_NEEDED)
end
-- // Function to handle player winning
local function playerWin(player)
if playersCollected[player.UserId] >= TOTAL_CUSTARDS_NEEDED then
print(player.Name .. " has found all custards and won!")
WinEvent:FireClient(player) -- Tell client they won
-- TODO: Implement actual win sequence (teleport, show win screen, etc.)
-- Reset player's count or prepare for next round if applicable
-- playersCollected[player.UserId] = 0
-- updatePlayerUI(player) -- Update UI to show 0/X for next round
end
end
-- // Function to handle player losing (conceptional - monster would trigger this)
-- This function would likely be called by a monster script upon killing a player
local function playerLose(player)
print(player.Name .. " was caught!")
LoseEvent:FireClient(player) -- Tell client they lost
-- TODO: Implement actual lose sequence (show lose screen, respawn, etc.)
-- Reset player's count or handle game over state
-- playersCollected[player.UserId] = 0
-- updatePlayerUI(player) -- Update UI
end
-- You'd call playerLose(player) from your monster script when it gets the player.
-- // Event Handler for Custard Collection
CustardCollectedEvent.OnServerEvent:Connect(function(player, custardPart)
-- Basic sanity checks
if not player or not player:IsA("Player") then return end
-- Optional: check if custardPart is valid and exists in Workspace Custards folder
-- if not custardPart or not custardPart.Parent or not custardPart.Parent:IsA("Folder") or custardPart.Parent.Name ~= "Custards" then return end
local userId = player.UserId
-- Initialize count for player if they haven't collected yet
if not playersCollected[userId] then
playersCollected[userId] = 0
end
-- Increment count
playersCollected[userId] = playersCollected[userId] + 1
print(player.Name .. " collected a custard. Total: " .. playersCollected[userId])
-- Update the player's UI
updatePlayerUI(player)
-- Check for win condition
if playersCollected[userId] >= TOTAL_CUSTARDS_NEEDED then
playerWin(player)
end
-- Note: The actual removal/hiding of the custard part is handled by the CollectScript
end)
-- // Handle new players joining
Players.PlayerAdded:Connect(function(player)
print(player.Name .. " joined. Initializing campaign state.")
playersCollected[player.UserId] = 0 -- Initialize count for new player
-- Wait for character to load before updating UI
player.CharacterAdded:Connect(function(character)
updatePlayerUI(player) -- Show initial UI
end)
updatePlayerUI(player) -- Also update immediately in case character loads fast
end)
-- // Handle players leaving
Players.PlayerRemoving:Connect(function(player)
-- Clean up player data when they leave
if playersCollected[player.UserId] then
playersCollected[player.UserId] = nil
print(player.Name .. " left. Campaign state removed.")
end
end)
-- // Optional: Function to start the campaign (e.g., after a lobby or countdown)
-- local function startCampaign()
-- print("Campaign Started!")
-- -- Reset all player counts
-- for userId in pairs(playersCollected) do
-- playersCollected[userId] = 0
-- -- Find player object by userId if needed for UI update
-- local player = Players:GetPlayerByUserId(userId)
-- if player then
-- updatePlayerUI(player)
-- end
-- end
-- -- TODO: Teleport players to start locations
-- -- TODO: Enable monster AI (if applicable)
-- end
-- // You could call startCampaign() here or from another script
-- startCampaign()
print("CampaignManager script loaded.")
3. Item Script (Workspace/CustardPart/CollectScript)
This script handles the interaction when a player touches a custard.
-- // Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
-- // Events
local EventsFolder = ReplicatedStorage:WaitForChild("Events")
local CustardCollectedEvent = EventsFolder:WaitForChild("CustardCollectedEvent")
-- // Configuration
local COOLDOWN = 1 -- Cooldown in seconds to prevent multiple touches triggering the event
-- // State
local isCollected = false -- Flag to prevent collecting multiple times
-- // Function to handle touch
local function onPartTouched(hit)
-- Check if the part has already been collected
if isCollected then return end
-- Check if the hit part is part of a player's character
local character = hit.Parent
local player = Players:GetPlayerFromCharacter(character)
if player then
-- Prevent further collection until cooldown or removal
isCollected = true
print("Custard touched by " .. player.Name)
-- Fire the event to the server
CustardCollectedEvent:FireServer(script.Parent) -- Pass the Part object itself (optional, server mostly needs the player)
-- Visual feedback: Hide the custard
script.Parent.Transparency = 1
script.Parent.CanCollide = false
-- Optional: Destroy the part after a delay
-- game.Debris:AddItem(script.Parent, 5) -- Destroy after 5 seconds
-- Reset cooldown (or just rely on isCollected and the server handling counts)
-- Since the server tracks which player collected and the part is hidden/gone,
-- a local cooldown isn't strictly necessary if `isCollected` is used and the part is removed.
-- If you want the part to reappear or be interactable again later, you'd need a timer here
-- and potentially re-enable collision/visibility.
end
end
-- // Connect the touch event
script.Parent.Touched:Connect(onPartTouched)
print("CollectScript loaded for " .. script.Parent.Name)
-- Optional: If the game resets or a new round starts, you might need a way to reset `isCollected`
-- and make the part visible/collidable again. This would likely involve a server script signaling
-- all item scripts or having the CampaignManager handle part visibility directly.
4. UI Script (Client-Side: StarterGui/ObjectiveGui/ObjectiveScript)
This script updates the UI text label based on server events.
-- // Services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
-- // Events
local EventsFolder = ReplicatedStorage:WaitForChild("Events")
local UpdateUIEvent = EventsFolder:WaitForChild("UpdateUIEvent")
local WinEvent = EventsFolder:WaitForChild("WinEvent")
local LoseEvent = EventsFolder:WaitForChild("LoseEvent")
-- // UI Elements
local ObjectiveLabel = script.Parent:WaitForChild("ObjectiveLabel") -- Make sure this matches your TextLabel name
-- // Function to update the objective text
local function updateObjectiveText(collected, total)
ObjectiveLabel.Text = "Custards: " .. collected .. "/" .. total
ObjectiveLabel.Visible = true -- Make sure the UI is visible
end
-- // Event handler for UI updates from the server
UpdateUIEvent.OnClientEvent:Connect(function(collected, total)
updateObjectiveText(collected, total)
end)
-- // Event handler for winning
WinEvent.OnClientEvent:Connect(function()
ObjectiveLabel.Text = "You Found All Custards! You Win!"
-- TODO: Add more win effects (sound, animation, separate win screen)
end)
-- // Event handler for losing
LoseEvent.OnClientEvent:Connect(function()
ObjectiveLabel.Text = "You Were Caught! Game Over!"
-- TODO: Add more lose effects (sound, jump scare, separate lose screen)
end)
-- // Initial setup (might be updated by the server shortly after joining)
ObjectiveLabel.Text = "Loading objectives..."
ObjectiveLabel.Visible = true
print("ObjectiveScript loaded.")
-- Note: A full game UI would need more states (main menu, pause, inventory, etc.)
How this Framework Works:
- Player Joins: The
CampaignManager (ServerScriptService) detects the new player, initializes their custard count to 0, and fires the UpdateUIEvent to that player's client.
- UI Loads: The
ObjectiveScript (StarterGui) receives the UpdateUIEvent and sets the ObjectiveLabel text to "Custards: 0/X".
- Player Finds Custard: The player's character touches a
CustardPart.
- Item Script Triggers: The
CollectScript inside the CustardPart detects the touch, verifies it's a player, sets its isCollected flag, makes the part invisible/non-collidable, and fires CustardCollectedEvent to the server.
- Server Updates State: The
CampaignManager receives CustardCollectedEvent, identifies the player, increments their count in the playersCollected table, and fires UpdateUIEvent back only to that specific player.
- UI Updates Again: The
ObjectiveScript receives the new UpdateUIEvent and updates the ObjectiveLabel (e.g., "Custards: 1/X").
- Win Condition: When a player's count in
playersCollected reaches TOTAL_CUSTARDS_NEEDED, the CampaignManager fires the WinEvent to that player's client.
- Win UI: The
ObjectiveScript receives WinEvent and displays a win message.
- Lose Condition (Conceptual): A separate monster script (which you would need to create) would detect when it kills a player. That monster script would then call the
playerLose() function defined in the CampaignManager, passing the player object.
- Lose UI: The
playerLose function fires the LoseEvent to the player's client. The ObjectiveScript receives LoseEvent and displays a lose message.
To Build a Full Campaign from This:
- Map Design: Create a detailed, atmospheric map.
- Custard Placement: Place
CustardPart objects throughout the map. Make sure each has the CollectScript inside it.
- Monster AI: This is the most complex part. You'll need scripts that:
- Move the monster (patrolling, chasing).
- Detect players (line of sight, proximity).
- Trigger chase behavior.
- Kill players on contact (set player's
Humanoid.Health = 0).
- Crucially, the monster script would need a way to signal the
CampaignManager when a player is caught, likely by calling the playerLose() function in the server script or firing a dedicated server event.
- Sound Effects: Implement ambient sounds, monster sounds, jumpscare sounds.
- Lighting: Use dark lighting, fog, flashlights (client-side scripting needed).
- Jumpscares: Trigger visual or audio events at specific points.
- Start/End Game Flow: Add lobby, countdowns, proper win/lose screens, and game resets.
- Animations: Give the monster and potentially the player character custom animations.
- Network Optimization: Ensure your scripts are efficient, especially the monster AI, to avoid lag.
This script framework provides the core objective tracking and UI necessary for a simple find-the-items horror game, forming a base for your Slendytubbies 3-inspired campaign. Good luck!