Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Sound not playing when a part is clicked? [closed]

Asked by 9 years ago

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
0
I got this in the output: 10:55:10.141 - Unable to cast value to std::string Raven_Caedes 95 — 9y

Locked by MessorAdmin, woodengop, drew1017, and alphawolvess

This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.

Why was this question closed?

4 answers

Log in to vote
0
Answered by 9 years ago

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 :)

Ad
Log in to vote
0
Answered by 9 years ago

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)
Log in to vote
0
Answered by 9 years ago

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

Log in to vote
0
Answered by 9 years ago

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)
0
On line 18, you have an extra "." Validark 1580 — 9y
0
Thanks for catching that aquathorn321 858 — 9y