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,

1game.Workspace.LobbyMusic.Touched:connect(function(obj)
2if obj.Parent:FindFirstChild("Humanoid") ~= nil then
3script.Parent:Play()
4obj.Parent.Humanoid.Died:connect(function()
5script.Parent:Stop()
6end)
7end
8end)

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...

01workspace.LobbyMusic.Touched:connect(function(obj)
02    local Model = obj.Parent
03    if Model:FindFirstChild("Humanoid") then
04        script.Parent:Play()
05        Model.Humanoid.Died:connect(function()
06            script.Parent:Stop()
07        end)
08    end
09end)
10workspace.LobbyMusic.TouchEnded:connect(fucntion(obj) -- Fires when a part moves off
11    local Model = obj.Parent -- The parent of the part that touched workspace.LobbyMusic
12    if Model:FindFirstChild("Humanoid") then -- Checks if Model is a player
13        script.Parent:Stop() -- Stops the music
14    end
15end)

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:

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

This script stops the music!

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

Answer this question