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