Play music in sound regions only if player RootPart is not anchored

I want to make it so that if the player’s HumanoidRootPart is anchored, the songs in Sound Areas can play.

Code:

local sound = Instance.new("Sound")

sound.Looped = true

sound.Parent = script


local char = game.Players.LocalPlayer.Character
local humanoid = char:WaitForChild("Humanoid")
local root = char:WaitForChild("HumanoidRootPart")


local areasGroup = workspace:WaitForChild("Areas")


humanoid.Touched:Connect(function(touchedPart)
	if root.Anchored == true then
		sound:Stop()
	end
	
	if touchedPart.Parent == areasGroup and root.Anchored == false then
		
		sound.SoundId = touchedPart.Music.SoundId
		
		sound:Play()
		
		touchedPart.TouchEnded:Connect(function(touchEndedPart)
			
			if touchEndedPart.Parent == char then 
				
				sound:Stop()
			end
		end)
	end
end)

What is the issue you have?
I see you create a Sound, but I don’t see you putting an ID for the Sound in the script.

If the sound is in areas you should put the Sound in a Part the size of each area and play it there, then use your Anchored/Unanchored script to turn it on and off.

1 Like

I believe that using a .Touched event doesnt fit an Area only music player as it is not effiecient at recording collisions inside of the part, Consider using workspace:GetPartsInAPart() instead.

2 Likes

I wrote it on wrong script, that i forget to delete :skull:
I’m so sorry for wasting your time.

Does that mean that solution was found? If so put the checkmark on the answer, so that this thread won’t continue.

1 Like

I have issue in other script. It doesn’t play if humanoid anchored = false

local plr = game.Players.LocalPlayer
local char = plr.Character
local root = char:FindFirstChild("HumanoidRootPart")

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 and root.Anchored == false then

				sound:Play()
			end

		else

			sound:Stop()
		end
	end
end)
1 Like

You didn’t waste my time, but here’s some new questions:
Why not use Parts for your Sound regions, with a Sound playing in each one.
When the player Root is Anchored by your script, check to see what Part the player is touching (of if you want all sound parts to stop playing reference all of them) and then Stop the Sound(s)?

Seems a whole lot more efficient to me.

I solved the problem by simply changing the RootPart’s CFrame.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.