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

Region3 music player identifies players but doesn't play music?

I have a region in which, if a player is found, it should play music for that player. The region works detects when a player is present, however, the sound does not play, even though I don't get an error from this script. This is all code in a script in ServerScriptService, there is a folder in StarterGui called SoundRegions containing the sounds which need to be played, and there is a folder in workspace titled SoundRegions which holds models of the name as the regions and the models also hold parts titled r1 and r2 used to make the region.

Image of the folders and objects: http://imgur.com/FQnCoJS

(The script is in serverscriptservice)

Also as of right now there is only one region because i want to get the script right before i add more but i dont know what is wrong.

The code: (with comments to help explain the process. probably easier to understand when looking at just the source)

local SoundRegWS = workspace:FindFirstChild("SoundRegions") --folder containing models which each hold two parts, r1 and r2, to make a region
local found = false --variable checking if a player is in a region

game.Players.PlayerAdded:Connect(function(player) -- get player when they join
	player.CharacterAdded:Connect(function(character) -- get player character when it appears
		while wait(1) do -- every second scan regions
			for i, v in pairs(SoundRegWS:GetChildren()) do -- v = models in SoundRegions folder, get the children of the models one by one
				found = false -- set player found in region to false initially
				local sound = player:WaitForChild("PlayerGui"):WaitForChild("SoundRegions")[v.Name] -- sound to play has same name as region, hence v.Name
				local region = Region3.new(v.r1.Position, v.r2.Position) -- region created using parts r1 and r2, found in the model, v
				local partsInRegion = workspace:FindPartsInRegion3WithWhiteList(region, character:GetDescendants()) -- scan for character parts
				for i, part in pairs(partsInRegion) do --redundant but it comes from code changes, checks for player parts in region again
					if part.Parent:FindFirstChild("Humanoid") ~= nil then --if player part found then
						print("Player found in region "..part.Parent.Name)
						found = true --player in region = true
						break --goto next bit
					else
						found = false --player not found, search regions again
						print("Player not found")
					end
				end
				if found == true then --if player in region then
					if sound.IsPlaying == false then --check if sound is already playing
						sound:Play() --if not, play
						break --wait for when the player leaves the region
					end
				else
					sound:Stop() --if player not in region, stop music
				end
			end
		end	
	end)
end)

EDIT: I have tried adding another region to see if that was the issue but nothing changed.

1 answer

✓ Accepted answer

Do you want the sound to play for everyone, or just for the player?

If you're storing the music in the player's StarterGui, then you shouldn't be using a server script in the ServerScriptService. Since you're saying that the region works and that it is identifying the player, but the problem is just with the music, try any one of these solutions depending on what you're trying to do.

If you want everyone to hear it: Move the sounds out of the StarterGui and place it into the server. If a server-side script uses :Play() on a non-3D (e.g. not in a part) sound, everyone will hear it.

If you want only the player to hear it: Move contents of the script into a LocalScript. Place the LocalScript somewhere in the player (such as in the StarterGui where the sounds are). If the LocalScript's code plays the sound, only its player will hear the sound being played.

If you want any player in a given range to hear it: You don't really need a region3 for this. If a Sound object is contained in a part, it automatically becomes a "3D" sound, and you can configure the maximum distance that it's audible.

If you want more of an explanation for why the location of the sound & the type of script matters, here's the relevant Wiki page: https://developer.roblox.com/en-us/articles/Roblox-Client-Server-Model

If this helped, please mark this correct or upvote it, but if you still need help, please leave a comment :)

1 comment

0

Thank you so much! After your suggestion of putting it into a localscript, I rethought my approach and upon tweaking the code a little bit, it works!

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