Here's a complete script that creates a text-to-speech system with a GUI button and text input. This will generate and play the sound when clicked:
lua
-- Create a ScreenGui
local ScreenGui = Instance.new("ScreenGui")
ScreenGui.Parent = game.Players.LocalPlayer:WaitForChild("PlayerGui")
-- Create the main frame
local Frame = Instance.new("Frame")
Frame.Size = UDim2.new(0, 300, 0, 200)
Frame.Position = UDim2.new(0.5, -150, 0.5, -100)
Frame.BackgroundColor3 = Color3.fromRGB(45, 45, 45)
Frame.BorderSizePixel = 0
Frame.Parent = ScreenGui
-- Create text input
local TextBox = Instance.new("TextBox")
TextBox.Size = UDim2.new(0.9, 0, 0.4, 0)
TextBox.Position = UDim2.new(0.05, 0, 0.1, 0)
TextBox.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
TextBox.TextColor3 = Color3.fromRGB(0, 0, 0)
TextBox.PlaceholderText = "Enter text to speak..."
TextBox.TextSize = 14
TextBox.TextWrapped = true
TextBox.Parent = Frame
-- Create speak button
local Button = Instance.new("TextButton")
Button.Size = UDim2.new(0.9, 0, 0.2, 0)
Button.Position = UDim2.new(0.05, 0, 0.6, 0)
Button.BackgroundColor3 = Color3.fromRGB(0, 120, 215)
Button.TextColor3 = Color3.fromRGB(255, 255, 255)
Button.Text = "Speak"
Button.TextSize = 18
Button.Parent = Frame
-- Create the TTS function
local function textToSpeech(text)
-- Get services
local TTS = game:GetService("TextToSpeechService")
local SoundService = game:GetService("SoundService")
-- Create sound instance
local sound = Instance.new("Sound")
sound.Parent = SoundService
-- Generate TTS
local success, soundId = pcall(function()
return TTS:Synthesize(text, Enum.TextToSpeechVoiceType.Female1)
end)
if success then
sound.SoundId = soundId
sound:Play()
-- Clean up sound after playing
sound.Ended:Connect(function()
sound:Destroy()
end)
else
warn("Failed to generate TTS:", soundId)
end
end
-- Connect button to TTS function
Button.MouseButton1Click:Connect(function()
local text = TextBox.Text
if text ~= "" then
textToSpeech(text)
end
end)
-- Add some styling
local UICorner = Instance.new("UICorner")
UICorner.CornerRadius = UDim.new(0, 8)
UICorner:Clone().Parent = Frame
UICorner:Clone().Parent = TextBox
UICorner:Clone().Parent = Button
-- Add title
local Title = Instance.new("TextLabel")
Title.Size = UDim2.new(1, 0, 0.1, 0)
Title.Position = UDim2.new(0, 0, 0, 0)
Title.BackgroundTransparency = 1
Title.TextColor3 = Color3.fromRGB(255, 255, 255)
Title.Text = "Text to Speech"
Title.TextSize = 18
Title.Parent = Frame
-- Make frame draggable
local UserInputService = game:GetService("UserInputService")
local dragging
local dragInput
local dragStart
local startPos
local function update(input)
local delta = input.Position - dragStart
Frame.Position = UDim2.new(startPos.X.Scale, startPos.X.Offset + delta.X, startPos.Y.Scale, startPos.Y.Offset + delta.Y)
end
Frame.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch then
dragging = true
dragStart = input.Position
startPos = Frame.Position
input.Changed:Connect(function()
if input.UserInputState == Enum.UserInputState.End then
dragging = false
end
end)
end
end)
Frame.InputChanged:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseMovement or input.UserInputType == Enum.UserInputType.Touch then
dragInput = input
end
end)
UserInputService.InputChanged:Connect(function(input)
if input == dragInput and dragging then
update(input)
end
end)
To use this script:
Create a new LocalScript in StarterPlayerScripts
Copy and paste the entire code into the script
Play test the game
Features of this script:
Creates a draggable GUI window
Has a text input box where you can type your message
Includes a "Speak" button that generates and plays the TTS
Uses the female voice (you can change this to Male1 if preferred)
Automatically cleans up sound objects after playing
Has error handling for failed TTS generation
Includes some basic styling with rounded corners
The GUI will appear in the center of the screen and can be dragged around. Type your text in the box and click "Speak" to hear it.
Note: Make sure the following services are enabled in Game Settings:
TextToSpeechService
SoundService
Also, remember that TTS might not work in Studio sometimes but should work when the game is published.
User
how to get voice ids in roblox studio ?
Assistant
In Roblox Studio, you can get voice/sound IDs in several ways:
Using Roblox Audio Library
Go to View > Toolbox
Click on the Audio tab
Search for sounds you want
Click on a sound to see its ID
The ID will be in the format "rbxasset://sounds/[ID]" or just a number
Upload Your Own Audio
Go to the Roblox website
Click on Create > Sounds
Upload your audio file (must be MP3 or OGG)
After uploading, you'll get an asset ID
Note: This now requires Robux to upload audio
Built-in Roblox Sounds
Here are some common built-in sound IDs you can use:
lua
-- Example of some default Roblox sound IDs
local commonSounds = {
Click = "rbxasset://sounds/click.mp3",
Victory = "rbxasset://sounds/victory.mp3",
Snap = "rbxasset://sounds/snap.mp3",
Page = "rbxasset://sounds/page-turn.mp3",
Explosion = "rbxasset://sounds/explosion.mp3"
}
Using Sound Objects
lua
-- Example of creating a sound
local sound = Instance.new("Sound")
sound.SoundId = "rbxasset://sounds/click.mp3" -- or your sound ID
sound.Parent = game.Workspace
sound:Play()
Finding Sound IDs in Games
lua
-- Script to print all sound IDs in workspace
local function findSounds(parent)
for _, object in pairs(parent:GetDescendants()) do
if object:IsA("Sound") then
print(object.Name .. ": " .. object.SoundId)
end
end
end
findSounds(game.Workspace)
Important Notes:
Many older sound IDs may no longer work due to Roblox's audio privacy changes
Uploading new audio requires Robux
Sound IDs can be in different formats:
Full URL: "rbxassetid://1234567"
Just the number: 1234567
Built-in sounds: "rbxasset://sounds/name.mp3"
Some sounds might be copyrighted or restricted
Test sounds before using them in your game
Keep volume levels appropriate for your game
To use a sound ID in your script:
lua
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://[YOUR_ID_HERE]"
sound.Volume = 1 -- 0 to 10
sound.Parent = game.Workspace -- or where you want to place it
sound:Play()
For voice chat specifically:
Roblox has a built-in voice chat system that doesn't use IDs
If you want to use custom voices, you'll need to upload them as audio files
Consider using the TextToSpeechService for generated voices instead of pre-recorded audio
Remember that Roblox's audio system has strict policies about what can be uploaded, so make sure to review their terms of service before uploading custom audio.