Of course! This is a classic and fun obby idea. I'll guide you through the whole process, from setting up your piano in the game to writing the code.
We will use a modern and efficient method called CollectionService. This lets us "tag" our parts (as "WhiteKey" or "BlackKey") and manage them all with a single script, which is much better than putting a separate script inside every single key.
Here is the full guide.
Part 1: Setting Up Your Piano in Roblox Studio
-
Create Your Keys:
- Create a
Part for a white key. Make it flat, rectangular, and color it white.
- Create another
Part for a black key. Make it smaller, color it black, and place it slightly above a white key.
- Make sure all your key parts are Anchored. You can select them all and check the "Anchored" box in the Properties window.
-
Organize Your Keys:
- In the Explorer window, create a
Model.
- Rename the model to something like "Piano".
- Drag all of your white and black key parts into this "Piano" model. This keeps your game organized.
-
Tag Your Keys (The Most Important Step!):
- Go to the VIEW tab at the top of Studio and open the Tag Editor.
- In the Tag Editor window, click the "Add Tag" button (it looks like a little tag with a plus sign).
- Type
WhiteKey and press Enter. Click the color box to make it blue or another color to help you see it.
- Create another tag called
BlackKey. Make it red.
- Now, select all of your white key parts in the 3D view or the Explorer.
- In the Tag Editor, click the checkbox next to the
WhiteKey tag. All your selected parts will now have this tag.
- Do the same for the black keys: select all of your black key parts and check the box next to the
BlackKey tag.
Part 2: Getting the Sounds
You need two sound effects.
- Go to the Creator Marketplace in Studio (under the HOME tab).
- Select the Audio category.
- Search for a "Piano Note" or "C4 Note" for the white keys. When you find one you like, click the "Insert" button. This will add a
Sound object to your Workspace.
- Search for a "Wrong Note" or "Buzzer" sound for the black keys. Insert it as well.
- Find the
Sound objects in your Explorer, and copy their "SoundId" from the Properties window. You'll need these for the script. You can delete the sound objects from the workspace after copying the IDs.
Part 3: The Code
This single script will handle everything.
- In the Explorer, hover over ServerScriptService and click the
+ icon.
- Select Script.
- Rename the script to something like
PianoManager.
- Delete the default
print("Hello world!") and paste the following code inside.
--[[
PianoManager Script
Handles all logic for the piano keys in the obby.
- White keys glow and play a sound on touch.
- Black keys play a death sound and kill the player on touch.
]]
-- Services
local CollectionService = game:GetService("CollectionService")
local Players = game:GetService("Players")
--// CONFIGURATION //--
-- Paste your Sound IDs here!
local WHITE_KEY_SOUND_ID = "rbxassetid://184223129" -- Default Piano C4 Note
local BLACK_KEY_SOUND_ID = "rbxassetid://132142137" -- Default Buzzer Sound
-- Glow effect settings
local GLOW_COLOR = Color3.new(0.5, 1, 1) -- A nice cyan glow
local GLOW_DURATION = 0.5 -- How many seconds the glow lasts
--// END CONFIGURATION //--
-- Function to handle what happens when any key is touched
local function onKeyTouched(hitPart)
-- First, check if the thing that touched the key is part of a player's character
local character = hitPart.Parent
local humanoid = character:FindFirstChildOfClass("Humanoid")
-- If it's not a player with a Humanoid, do nothing
if not humanoid or humanoid.Health <= 0 then
return
end
-- Get the actual key part that was stepped on
-- The Touched event gives us the part that did the touching (hitPart),
-- so 'self' refers to the key part the script is connected to.
local keyPart = self
-- Check the key's tag to decide what to do
if keyPart:HasTag("WhiteKey") then
-- It's a white key! Make it glow and play a sound.
-- Play Sound
local sound = keyPart:FindFirstChildOfClass("Sound")
if sound then
sound:Play()
end
-- Make it Glow
local highlight = Instance.new("Highlight")
highlight.FillColor = GLOW_COLOR
highlight.OutlineColor = GLOW_COLOR
highlight.FillTransparency = 0.5
highlight.Parent = keyPart
-- Remove the glow after a short time
task.delay(GLOW_DURATION, function()
highlight:Destroy()
end)
elseif keyPart:HasTag("BlackKey") then
-- It's a black key! Play a sound and kill the player.
-- Play Sound
local sound = keyPart:FindFirstChildOfClass("Sound")
if sound then
sound:Play()
end
-- Kill the player
humanoid.Health = 0
end
end
-- Function to set up a single key
local function setupKey(keyPart)
-- Create and parent the sound instance to the key
local sound = Instance.new("Sound")
sound.Parent = keyPart
-- Assign the correct sound ID based on the tag
if keyPart:HasTag("WhiteKey") then
sound.SoundId = WHITE_KEY_SOUND_ID
elseif keyPart:HasTag("BlackKey") then
sound.SoundId = BLACK_KEY_SOUND_ID
end
-- Connect the Touched event to our function
keyPart.Touched:Connect(onKeyTouched)
end
-- Go through all existing parts with the tags and set them up
for _, part in ipairs(CollectionService:GetTagged("WhiteKey")) do
setupKey(part)
end
for _, part in ipairs(CollectionService:GetTagged("BlackKey")) do
setupKey(part)
end
-- This part is optional but good practice:
-- It makes sure that if you add new keys WHILE the game is running, they will also work.
CollectionService:GetInstanceAddedSignal("WhiteKey"):Connect(setupKey)
CollectionService:GetInstanceAddedSignal("BlackKey"):Connect(setupKey)
How to Customize
- Change Sounds: Go to the
CONFIGURATION section at the top of the script. Replace the rbxassetid://... numbers with the Sound IDs you copied from the Creator Marketplace.
- Change Glow: You can change the
GLOW_COLOR to any color you want using Color3.new(). The numbers are for Red, Green, and Blue, from 0 to 1. You can also change the GLOW_DURATION.
Now, just press Play and test it out! When you jump on a white key, it should glow and play a piano note. When you jump on a black key, it should play the buzzer sound and your character will die and respawn.