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

How to make this script play audio only once and stop when new audio needs to start?

local brick = script.Parent
local sound = script.Parent.Sound
debounce = false

brick.Touched:connect(function(hit)
player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player and not debounce then
debounce = true
play = sound:Clone()
play.Parent = player.PlayerGui
play:Play()
wait(5)
debounce = false
end
end)

Right now this script doesn't work properly. After touching another brick with the same script, the music from both bricks plays at the same time.

Changing this:

if player and not debounce then

into this:

if player and not debounce and player.PlayerGui.sound.Playing == false then

did not work.

This is how it should work:

After touching the brick, music plays.

After touching another brick that causes the same song to play, nothing happens.

After touching a brick that causes another song to play, the first song stops and the second song starts.

2 comments

0

so brick1 is touched, music plays, brick2 is touched music is played, but nothing happens to music, and brick3 touched so all music starts and second one starts right?

0

Brick1 makes music1 play. Brick2 also makes music1 play, so nothing changes. Brick3 makes music2 play and stops all other music from playing

2 answers

The following script tracks the current SoundId's being played for each user and changes according to which brick they step on.

--[[
	Brick1 makes music1 play.
	Brick2 also makes music1 play, so nothing changes.
	Brick3 makes music2 play and stops all other music from playing
]]

local Song = {
	[1] = workspace.Sound1;
	[2] = workspace.Sound2;
}

local Brick = {
	[1] = {Part = workspace.Brick1, Song = Song[1]};
	[2] = {Part = workspace.Brick2, Song = Song[1]};
	[3] = {Part = workspace.Brick3, Song = Song[2]};
};

-- Set up table with indices PlayerNames and values SongPlaying
local CurrentlyPlaying = {}

local function PartTouch(hit, num)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if player then

		local playerName = player.Name
		local previouslyPlaying = CurrentlyPlaying[playerName]

		if previouslyPlaying then
			previouslyPlaying:Stop()
		end

		if not previouslyPlaying or previouslyPlaying.SoundId ~= Brick[num].Song.SoundId then
			local newSong = Brick[num].Song:Clone()
			newSong.Parent = player.PlayerGui
			newSong:Play()

			CurrentlyPlaying[playerName] = newSong
		end
	end
end

local function ConnectionHelper(a)
	return function(hit)
		return PartTouch(hit, a)
	end
end

for a = 1, #Brick do
	Brick[a].Part.Touched:connect(ConnectionHelper(a))
end

4 comments

0

You should explain your code a bit more so he actually understands what is going on. I think objects are a lot simpler, too, otherwise I would've done tables ;p

0

The only problem is that if I explain my code this website's text formatter will ruin the look of the code

0

Explain it in the answer then lol

0

Sorry, but for some reason this script didn't work. Even after renaming the bricks and audio to have the same names as those in the script.

I think the big problem with your code is that everything is in a different script, and it can be hard to manage things that way.

If you wanted a time limit between touches, I have also added that in.


Put this code in a Script inside ServerScriptService or another entity in game.

local partsFolder = game.Workspace:WaitForChild("PartsFolder") -- the folder that holds all of the parts that gets touched
local parts = partsFolder:GetChildren() -- each part will have a sound inside them called 'Music'

game.Players.PlayerAdded:connect(function(p) -- when a player is added
	local val = Instance.new("ObjectValue") -- the 'debounce'
	val.Name = "CurrentSong" -- set it's name (for viewing only)
	local song = Instance.new("Sound",p.PlayerGui) -- makes a new sound that will play locally
	song.Name = "Music" -- sets it's name (for viewing only)
	local char -- for future reference
	p.CharacterAdded:connect(function(chara) -- when their character is added
		char = chara -- sets the value to the current character so this script works for new characters
	end)
	local cooldown = 5
	local cooling = false
	
	for _,v in pairs (parts) do -- goes through all the parts
		local music = v:FindFirstChild("Music") -- the sound inside the parts
		if music then -- if it exists
			v.Touched:connect(function(hit) -- when the part is touched
				if hit.Parent == char and cooling == false then -- if a player touched
					cooling = true -- sets the cooldown debounce
					if not val.Value or music.SoundId ~= val.Value.SoundId then -- if the song is eligible to play
						song:Stop() -- stops current music
						wait() -- for the repeat loop below
						val.Value = music -- sets the value
						song.SoundId = music.SoundId -- sets the SoundId
						song:Play() -- plays the music

						coroutine.resume(coroutine.create(function() -- creates a new thread so the code can wait properly
							wait(cooldown) -- waits however long the cooldown is
							cooling = false -- sets the cooldown debounce
						end))

						repeat wait() until song.IsPlaying == false -- wait()s until the song isn't playing
						if val.Value == music then -- if the song is the one inside this part
							val.Value = nil -- set the 'debounce' to nil
						end
					end
				end
			end)
		end
	end
end)

Hope I helped!

~TDP

15 comments

0

Tables > RobloxObjects

0

When a song is playing, after touching a brick that causes the same song to play the music starts over. Is there a way to prevent that? Adding "if song.SoundId ~= music.SoundId then" between lines 22 and 23 did not work.

0

Fixed, I forgot to put in a '.SoundId' in the mentioned if statement

0

Sadly, there are two more problems. First, music plays only in Studio, not in the actual game. Second, after a few seconds the music stops and none of the song-playing bricks works anymore.

0

I'm not sure how I can fix it without complete context and visual information (AKA roblox studio). Maybe you could PM EmeraldSlash with TC?

0

Umm... Sorry, but who is EmeraldSlash and what is TC?

0

EmeraldSlash is my main account (he got banned from SH :/) and TC is Team Create (so I can help you to my fullest extent).

0

Request sent. And thank you for helping.

0

np, I'm at school rn, so I will help you later today.

0

Umm... what time is it where you live? It's midnight here.

0

10:11 am for me. Saturday tomorrow for me, though.

0

So, will you be online at about 8 pm (your time)?

0

Yeah.

0

Are you there? Did you get the Roblox private message?

0

Yes. I have started editing :p

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