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

Broken script that play music in a area, but when you die or reset the area stop playing music?

I have a script that is supposed to play music in areas (the area are parts that are transparent and you can walk thought them), but when you reset or die the music does not play anymore in the area.Can someone plz help me.

code:

local plr = game.Players.LocalPlayer

local soundRegions = workspace:WaitForChild("SoundRegions")
soundRegions = soundRegions:GetChildren()

local soundManagement = {}

for _,region in pairs(soundRegions) do

	local info = {}

	local region3 = Region3.new(region.Position-(region.Size/2),region.Position+(region.Size/2))
	region.Transparency = 1

	info.Region = region3
	info.Sound = script.SoundRegions:FindFirstChild(region.name).Sound

	table.insert(soundManagement,info)

end

game:GetService("RunService").RenderStepped:Connect(function()

	for _,soundInfo in pairs(soundManagement) do

		local region = soundInfo.Region
		local sound = soundInfo.Sound
		local parts = workspace:FindPartsInRegion3WithWhiteList(region,plr.Character:GetDescendants())

		if #parts > 0 then

			if not sound.IsPlaying then

				sound:Play()

			end

		else

			sound:Stop()

		end

	end

end)


What I tried to do to fix it.

local plr = game.Players.LocalPlayer

local soundRegions = workspace:WaitForChild("SoundRegions")
soundRegions = soundRegions:GetChildren()

local soundManagement = {}

for _,region in pairs(soundRegions) do

	local info = {}

	local region3 = Region3.new(region.Position-(region.Size/2),region.Position+(region.Size/2))
	region.Transparency = 1

	info.Region = region3
	info.Sound = script.SoundRegions:FindFirstChild(region.name).Sound

	table.insert(soundManagement,info)

end

game:GetService("RunService").RenderStepped:Connect(function()

	for _,soundInfo in pairs(soundManagement) do

		local region = soundInfo.Region
		local sound = soundInfo.Sound
		local parts = workspace:FindPartsInRegion3WithWhiteList(region,plr.Character:GetDescendants())

		if #parts > 0 then

			if not sound.IsPlaying then

				sound:Play()

			end

			plr.Character.Humanoid.Died:Connect(function() 
				sound:Stop()
			end)

			sound:Stop()

		end

	end

end)


2 comments

0

Where is the script located?

0

in starter gui

1 answer

✓ Accepted answer

You need to get the players health. You can do this by checking their health property inside the humanoid object.

if (plr.Character.Humanoid.Health <= 0) then
	sound:Stop()
end

The <= is a math symbol. This means if the value is equal to or less then the value we are comparing to. In this instance, if the plr.Health is less then or equal to 0 then we just want to stop the sound.

Another option is by using the .Died event

plr.Character.Humanoid.Died:Connect(function() 
	sound:Stop()
end)

I suggest using my second piece of code, because in the first example you would need to have a while true do loop. This is not very good on performance. If this helped you, please accept this as the answer so other people can see this answer.

16 comments

0

Thank you so much!I have been trying to do that, but I suck at code.

0

Wait I am not sure where to put this code.

0

Put it at the end of your main script, after the sound is started playing.

0

Please accept this post as the solution, this helps other people find it faster.

0

The code does not fix the problom

0

What is the error?

0

When I put at the end of the entire script it does not do anything.

0

Put it after the if statement. I would need the whole script to tell you were to specifically put it.

0

Here entire code

0

local plr = game.Players.LocalPlayer local soundRegions = workspace:WaitForChild("SoundRegions") soundRegions = soundRegions:GetChildren() local soundManagement = {} for _,region in pairs(soundRegions) do local info = {} local region3 = Region3.new(region.Position-(region.Size/2),region.Position+(region.Size/2)) region.Transparency = 1 info.Region = region3 info.Sound = script.SoundRegi

0

Destroy the Else statement, after the end (That would end the If Statement) Put it right there after that end.

0

(Edit your original post to put all that code into a code block)

0

ok I did

0

Replace sound:Stop with that.

0

Yeah it doe snot work.It ok I turned all my songs in a random playlist.

0

I have nothing else to help with then, I know my script is working fine. I will double check when I get a chance.

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