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

How could I add music into a Block?

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

✓ Accepted answer

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)

1 comment

0

It very much did thank you!

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)

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