Sound Region LocalScript

  1. What do you want to achieve? Im tryna make a Sound Region meaning if your Character Attribute Location Changes then Depending on the Location Play A Music

  2. What is the issue? The Issue with my localscript is that If the Attribute changes it doesnt stop playing the current Music Immediately

  3. What solutions have you tried so far? Ive tried Looking everywhere for help

local plr = game.Players.LocalPlayer
local Character = plr.Character or plr.CharacterAdded:Wait()
local MusicsFolder = workspace.Musics



local Music = 0


local Song = nil
local CurrentNumber = 0

local oldLocation = Character:GetAttribute("Location")

while true do
	
	local currentLocation = Character:GetAttribute("Location")
		if currentLocation == "Outside" then
			-- Stop the music and play the new song
				warn("Location Exist and Playing oUTSIDE")
				Music = Music + 1
				CurrentNumber = Music
				if Music > 10 then
					Music = 1
				end
				if Music == 1 then
					Song = MusicsFolder.song01
				end
				Song = MusicsFolder["song0"..Music]
				while Song.TimeLength == 0 do
					wait()
				end
				Song:Play()
				Song.Ended:Wait()
		warn("Playing Next One" .. Song.Name) 
		elseif currentLocation == "InAngelShare" then
			warn("Playing Angel SHare")
			-- Stop the music and play the AngelShare song
		Song = MusicsFolder.AngelShare
			MusicsFolder.AngelShare:Play()
			MusicsFolder.AngelShare.Ended:Wait()
		end
		oldLocation = currentLocation
	wait(1)
end





So Basically i want it that if the Character Attribute Location == Outside then do all those stuffs but if it changes to InAngelShare then Stop Playing the Song Immediately and Play AngelShare music

Im begging anyone Please Help me

Use ZonePlus instead of a while loop to check for the player being inside or outside:

This is a recommendation.

Now, if you absolutely want to stick with your method, then that’s fine too.
The reason why your sound isn’t ending immediately is due to the fact that you have too many waits.

What I propose we do instead is we scrap the usage of a while loop, and instead use “:GetAttributeChangedSignal()”.

This way, we won’t have any delays with our script.

Here’s how we implement this:

local plr = game.Players.LocalPlayer
local Character = plr.Character or plr.CharacterAdded:Wait()
local MusicsFolder = workspace.Musics

local Music = 0
local Song = nil
local CurrentNumber = 0

Character:GetAttributeChangedSignal("Location"):Connect(function()
	local location = Character:GetAttribute("Location");
	
	if location == "Outside" then
		Song:Stop()
		
		Music = Music + 1
		CurrentNumber = Music
		if Music > 10 then
			Music = 1
		end
		if Music == 1 then
			Song = MusicsFolder.song01
		end
		Song = MusicsFolder["song0"..Music]
		
		Song.TimeLength = 0;
		Song:Play();
	elseif location == "InAngelShare" then
		Song:Stop()
		
		Song = MusicsFolder.AngelShare
		MusicsFolder.AngelShare:Play()
	end
end)

Let me know if this helped.

tysm u helped alot man thanks so much

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