Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Sound not playing when a part is clicked?

I'm having trouble getting a sound to play when you click the part.

if game.Workspace:WaitForChild(game.Workspace.StatueClick.ClickDetector.MouseClick)
	then local Ambient2 = Instance.new("Sound")
	Ambient2.Parent = game.workspace 
	Ambient2.SoundId = "rbxassetid://180919842"
	Ambient2:Play()
	Ambient2.Volume = 1
		end
	end
end

1 comment

0

I got this in the output: 10:55:10.141 - Unable to cast value to std::string

4 answers

You are using an if statement when you should be calling an event. Change it to this:

local button = game.Workspace:WaitForChild('StatueClick').ClickDetector
button.MouseClick:connect(function()
	if not game.Workspace:FindFirstChild('Ambient2') then
		local ambient2 = Instance.new('Sound', game.Workspace)
		ambient2.Name = 'Ambient2'
		ambient2.SoundId = 'rbxassetid://180919842'
		ambient2.Volume = 1
		ambient2:Play()
	end
end)

Hope this helps :)

It didn't work for some reason. What's the issue?

if game.Workspace:FindFirstChild("Sound") == nil then
    local Ambient = Instance.new("Sound")
    Ambient.Parent = game.workspace
    Ambient.SoundId = "rbxassetid://238895410"
    Ambient:Play()
    Ambient.Volume = 1
end
end
local button = game.Workspace:WaitForChild('StatueClick').ClickDetector
button.MouseClick:connect(function()
    if not game.Workspace:FindFirstChild('Ambient2') then
        local ambient2 = Instance.new('Sound', game.Workspace)
        ambient2.Name = 'Ambient2'
        ambient2.SoundId = 'rbxassetid://180919842'
        ambient2.Volume = 1
        ambient2:Play()
    end
end)

Do you have a click detector? if you do, then you need the audio in the brick so the script can process the sound.

Your problem might just have been you were creating the sound and playing it under the same if statement, whether it exists or not. If the sound already existed, then the button wouldn't play. I went ahead and fixed that for you. I got this script to work in studio.

local button = game.Workspace:WaitForChild('StatueClick').ClickDetector

if  not game.Workspace:FindFirstChild("Ambient")  then
    Ambient = Instance.new("Sound")
    Ambient.Name = "Ambient"
    Ambient.Parent = game.workspace
    Ambient.SoundId = "rbxassetid://238895410"
    Ambient.Volume = 1
end

if not game.Workspace:FindFirstChild('Ambient2') then
	ambient2 = Instance.new('Sound', game.Workspace)
	ambient2.Name = 'Ambient2'
	ambient2.SoundId = 'rbxassetid://180919842'
	ambient2.Volume = 1
end

button.MouseClick:connect(function()
	ambient2:Play()
end)

2 comments

0

On line 18, you have an extra "."

0

Thanks for catching that

This discussion is preserved as it was. New answers, comments, and votes are closed.