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

How could I add music into a Block?

Asked by 7 years ago

I have block with music in it. Once you touch it the music plays. I want the music to stop once you step off the block. How would I edit this to make it stop once UnTouched,

game.Workspace.LobbyMusic.Touched:connect(function(obj)
if obj.Parent:FindFirstChild("Humanoid") ~= nil then
script.Parent:Play()
obj.Parent.Humanoid.Died:connect(function()
script.Parent:Stop()
end)
end
end)

2 answers

Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

First of all, don't use game.Workspace, even though it's technically down to preference, workspace is much shorter and easier to use.

Second, you should tab your code, this makes it easier to read.

Third, you don't need the ~= nil on line 2, it's just redundant.

Fourth, there is an event called TouchEnded(), this fires when something moves off a part.

In context it would look like this...

workspace.LobbyMusic.Touched:connect(function(obj)
    local Model = obj.Parent
    if Model:FindFirstChild("Humanoid") then
        script.Parent:Play()
        Model.Humanoid.Died:connect(function()
            script.Parent:Stop()
        end)
    end
end)
workspace.LobbyMusic.TouchEnded:connect(fucntion(obj) -- Fires when a part moves off
    local Model = obj.Parent -- The parent of the part that touched workspace.LobbyMusic
    if Model:FindFirstChild("Humanoid") then -- Checks if Model is a player
        script.Parent:Stop() -- Stops the music
    end
end)

I hope this helps! (I've also tabbed code for you)

0
It very much did thank you! cooldrewbie 94 — 7y
Ad
Log in to vote
1
Answered by 7 years ago
Edited 7 years ago

Also if you want to click a part for music add a ClickDetector then add sound paste the ID in there, then add a script which is:

script.Parent.ClickDetector.MouseClick:Connect(function()
    script.Parent.Sound:Play()
end)

This script stops the music!

script.Parent.ClickDetector.MouseClick:Connect(function()
    script.Parent:Stop()
end)

Answer this question