Audio in StartCharacterScript isn’t working.
I just plays by zone (closer you get louder it gets) and it doesn’t even loop the audios and only one of it plays. Here’s the script roblox ai adjusted for me to help “fix” it.
local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local hrp = character:WaitForChild("HumanoidRootPart")
-- Path to your union inside Workspace > Folders > Buildings > Dance Party
local union = workspace:WaitForChild("Folders")
:WaitForChild("Buildings")
:WaitForChild("Dance Party")
:WaitForChild("MusicPlayer Union")
-- The two sounds inside the union
local music = union:WaitForChild("Music")
local humming = union:WaitForChild("Humming")
-- This sound instance will play LOCALLY for the player
local LocalSound = Instance.new("Sound")
LocalSound.Parent = character
LocalSound.Volume = 1
local currentZone = nil
local playing = false
-- Alternating play loop
local function playAlternating()
playing = true
while playing do
-- Play Music
LocalSound.SoundId = music.SoundId
LocalSound:Play()
LocalSound.Ended:Wait()
if not playing then break end
-- Play Humming
LocalSound.SoundId = humming.SoundId
LocalSound:Play()
LocalSound.Ended:Wait()
if not playing then break end
end
end
-- Stop everything
local function stopSounds()
playing = false
LocalSound:Stop()
end
-- Heartbeat loop checking if player stands on the union
game:GetService("RunService").Heartbeat:Connect(function()
-- cast downward
local ray = Ray.new(hrp.Position, Vector3.new(0, -5, 0))
local hitPart = workspace:FindPartOnRayWithWhitelist(ray, { union })
if hitPart == union then
-- enter zone
if currentZone ~= "MusicZone" then
currentZone = "MusicZone"
stopSounds()
task.wait() -- avoids overlap
playAlternating()
end
else
-- left zone
if currentZone ~= nil then
currentZone = nil
stopSounds()
end
end
end)
The script is supposed to work like this: Player enters the Union Part (two parts combined) and in that area the music script works and these two start palying turn by turn and once player leaves it stops.