Setting up Region3 to work with a server wide script

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

Utilizing region3 to enable the player to listen to a server-side music playlist when inside the said region.

  1. What is the issue? Include screenshots / videos if possible!
    I currently have regions set up to work with a client-side music player, but I’m so lost on trying to set it up to work with one particular region connecting to a server side script that handles its own playlist.

  2. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have a somewhat approach to this with:

local function SetPlayerRegion() --check if in region
	local char = LocalPlayer.Character

	if char then
		local inRegion = false
		
		for regionName, region3 in pairs(REGIONS) do
			local partsInRegion = workspace:FindPartsInRegion3WithWhiteList(region3, {char})
			--print(partsInRegion)
			if #partsInRegion > 0 then
				inRegion = true
				if ClientMusicZone.PlayerRegion ~= regionName then --check if changed region
					ChangeRegion(regionName)
					if regionName ~= "Club" then
						REMOTE:FireServer(char, ServerPlaylist)
					end
				end
				break
			else
				
			end
			wait(0.5) --makes the microprofiler happier
		end
		
		if not inRegion and ClientMusicZone.PlayerRegion then
			ChangeRegion(nil)
		end
	end
end

My issue with this is i’m unsure of what to actually set my parameters for firing the remote.

This is just example code of a server-side script I found for mostly testing purposes before actually editing the script to do what I actually want it to do, but I’ve found it difficult to connect the two.

Ideally since music is being handled by the server, shouldn’t it always be set to volume = 0 for a client not within the region and volume increased when region is entered?

local MODE = "random"

local MarketplaceService = game:GetService("MarketplaceService")

local audio = Instance.new("Sound")
audio.Name = "MusicAudio"
audio.Parent = workspace

local remote = game.ReplicatedStorage.Remotes.MusicZoneRemote
local songs = require(script.Songs)
local rand = Random.new(tick())
local currentIndex = 0
local function pickSong()
	local i
	if MODE == "cycle" then
		if currentIndex == #songs then
			currentIndex = 1
		else
			currentIndex = currentIndex + 1
		end
		i = currentIndex
	else
		i = rand:NextInteger(1, #songs)
	end
	
	return songs[i]
end

local nowPlaying = ""

local function getSongName(id)
	local data = MarketplaceService:GetProductInfo(id)
	nowPlaying = data.Name	
	return data.Name
end

remote.OnServerEvent:Connect(function(player,nowPlaying)
	remote:FireClient(player, nowPlaying)
end)

while true do
	local song = pickSong()	
	print("Now Playing:", song)
	remote:FireAllClients(getSongName(song))
	audio.SoundId = "rbxassetid://" .. song
	audio.TimePosition=0
	audio:Play()
	audio.Ended:Wait()
	audio:Stop()
end

Side note for the server script, the playlist for this is handled within a module script.

My main hurdle is just getting them to connect to each other. I should be able to figure out the rest as far as modifying the code to behave with my UI and such as needed.

1 Like

Well, I mean you don’t really need to make it like that. I mean you can use:

local part = game.workspace.part
part.Touched:Connect(function(plr)
--- Start the music
part.TouchedEnded:Connect(function()
--- End the music
  end)
end)

Edit: Then just randomize it, and put this local script in starterplayer scripts

1 Like

Well, with how my regions are set up, if a player isn’t in any defined region, there’s a default playlist that plays – so there’s always something being played. If I were to use that method wouldn’t there just be overlapping tracks?

1 Like

Well no, if you stop the current music playing or just make the server script a local script in starterplayer that auto plays music and you can stop any audio playing via the sound:Pause()

This is where I get confused because running two separate music systems, one being client sided, and one being server sided. Would I start out the script with setting the sound volume to 0, touch a part, pause the client music, and raise the volume of the sever side playlist? This is also all tied to a UI as well…so I’d need a way to configure it to show up its own specific version of the UI. If that makes any sense.

This was the big reasoning behind wanting to use region3, because the system is already made for region3.

Do it in the same script and use break to break the music

so, i’m determined and dumb so I’m still working on getting the regions to work, however when I added the new region, the main module isn’t picking up on the region existing and is returning Nil. Any ideas on what that is?