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 comment
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!