I have minimal scripting skills, so I genuinely have no idea how to fix this issue; This is a simple ambient zone system that I’ve tried to modify.
Whenever one of these parts is touched, the “ambient” audio is supposed to play, the issue is that for some reason it only works once. (The audios are supposed to override each other like in the first part of the video)
I’ve tried literally everything, looking through devforum, youtube, chatgpt, scripting-help channels in external servers.
local SoundIDs = {11669482876, 11669484641, 11144783122, 11144787496, 11144773544, 11144779480, 11669486868, 11144794378, 11144791329, 11669481069, 11144776236, 11669482876};
local Volume = 0.50;
local isPlaying = false;
local function CreateSound(soundID, volume, parent)
local sound = Instance.new("Sound", parent);
sound.Volume = volume;
sound.Name = "_PlayOnTouchedSound";
sound.SoundId = "rbxassetid://".. soundID;
sound.Looped = false;
sound.Ended:Connect(function()
sound:Destroy()
isPlaying = false;
end)
sound:Play()
isPlaying = true;
end;
local currentIndex = 1;
script.Parent.Touched:Connect(function(other)
if other and game.Players:GetPlayerFromCharacter(other.Parent) then
local player = game.Players:GetPlayerFromCharacter(other.Parent);
local PlayerGui = player.PlayerGui
if not isPlaying then
for _, sound in ipairs(PlayerGui:GetChildren()) do
if sound.Name == "_PlayOnTouchedSound" then
sound:Destroy()
end
end
CreateSound(SoundIDs[currentIndex], Volume, player.PlayerGui);
currentIndex = currentIndex % #SoundIDs + 1;
isPlaying = true;
end;
end;
end);
script.Parent.TouchEnded:Connect(function(other)
if isPlaying then
for _, sound in ipairs(script.Parent.Parent.PlayerGui:GetChildren()) do
if sound.Name == "_PlayOnTouchedSound" then
sound:Destroy()
end
end
isPlaying = false
end
end)
game:GetService("SoundService").SoundEnded:Connect(function(sound)
if sound.Name == "_PlayOnTouchedSound" then
isPlaying = false
end
end)
I found the script above to be overly difficult to use and modify.
I re-worked it to make it simple to understand and to add/change zones:
local Players = game:GetService("Players")
local Player = Players.LocalPlayer or Players.PlayerAdded:Wait()
local Zones = game.Workspace:WaitForChild("Zones")
local Sounds = game:GetService("SoundService"):WaitForChild("Zones")
local PartsTouching = {}
local IsPlaying = false
for _, zone in pairs(Zones:GetChildren()) do
if zone:IsA("BasePart") then
zone.Touched:Connect(function(otherPart)
if otherPart.Parent.Name == Player.Name then
-- add parts to table
if not table.find(PartsTouching, otherPart) then
table.insert(PartsTouching, otherPart)
end
-- track bool
if IsPlaying == false then
IsPlaying = true
-- stop sounds
for _, sound in pairs(Sounds:GetChildren()) do
if sound:IsA("Sound") then
sound:Stop()
end
end
-- play sound
if Sounds:FindFirstChild(zone.Name) then
Sounds[zone.Name]:Play()
else
warn("SOUNDS | ERROR: Sound Not Found", zone.Name)
end
end
end
end)
zone.TouchEnded:Connect(function(otherPart)
if otherPart.Parent.Name == Player.Name then
-- remove parts from table
if table.find(PartsTouching, otherPart) then
table.remove(PartsTouching, table.find(PartsTouching, otherPart))
-- parts table empty
if #PartsTouching == 0 then
-- reset bool
IsPlaying = false
-- stop sound
if Sounds:FindFirstChild(zone.Name) then
Sounds[zone.Name]:Stop()
Sounds[zone.Name].TimePosition = 0
else
warn("SOUNDS | ERROR: Sound Not Found", zone.Name)
end
end
end
end
end)
end
end
While this does work, it is only partially of what I’m trying to achieve.
I want the zones to have some sort of looping playlist comprised of multiple audios. This is not exactly an “ambient” sound system it’s more of a soundtrack system.
Still not what I’m trying to achieve; here’s a video with a few issues I’ve noticed along with an example of what I really want to achieve with this script. (The example is the original system, and only has one audio [which is the problem]; as I’ve stated before, I want multiple audios, kind of like a playlist.)