Can you cut a part of a Sound?

Basically i have a sound and i don’t want the part at the end of it. Is there a way to cut it off or a way of detection when like TimePosition is equal or greater to set it back to 0 but without actually having a constant loop to keep on checking the TimePosition because the sound that i have can Pause and continue so there can not be a fixed time of it to reset it.

5 Likes

Yes this is what the code would look like.
This is from a ServerScript so the Stepped would have to be changed back to .RenderStepped if it is on a local script.

game:GetService("RunService").Stepped:Connect(function()
	local CutOffSound = 2 	--- The amount of seconds we want to cut out from the end
	if (Sound.TimeLength - Sound.TimePosition <= CutOffSound) then
		print("Cut sound")
		Sound.TimePosition = 0
	end
end)

Edit:
If you want to skip from the middle of the sound this is how it would be done.

game:GetService("RunService").Stepped:Connect(function()
	local CutOffSound = {3, 50} 	--- The first number is when you want to skip, the second number is where it skips to.
	if (Sound.TimePosition >= CutOffSound[1] and Sound.TimePosition < CutOffSound[2]) then
		Sound.TimePosition = CutOffSound[2]
	end
end)
12 Likes

I had the same problem and this was really helpful!

2 Likes

Yeah its on localscript. Also wouldn’t this thow be performance expensive as it fires every frame? Isn’t possible to have some kind of detection i tried using GetPropertyChangedSignal on TimePosition but it doesn’t work on that. Any other idea? or that’s like the only way?

2 Likes

See line 17 from the bottom up, v[3][1] is the startPosition, v[3][2] is the endPosition

2 Likes

Yes, you can make it trigger on a .Played event and make it wait a certain amount of time to change the time position of the sound.

This is what it would look like.

local CutTimes = {3, 10} --- First number is how long it should play in seconds before it skips to the 2nd number
Sound.Played:Connect(function()
	wait(CutTimes[1])
	Sound.TimePosition = CutTimes[2]
end)
3 Likes

Trimming an audio in Roblox is possible by using playback regions. You can enable them through the properties window with the sound selected. A new category called “Regions” should appear in the properties window and there are 2 properties that both take in NumberRange values. LoopRegion will play the audio in that range after the sound has been looped; it only applies when you have the looped property set to true. PlaybackRegion is the overall range on where the sound starts playing and where it ends and overrides the LoopRegion if the min of LoopRegion is less than the min of PlaybackRegion. Once the TimePosition property reaches the max value of that region, the sound will set the TimePosition to the min value of the loop region if looped is set to true or to the min value of the PlaybackRegion. Heres an example on how you can use PlaybackRegions.

local loopRegion = NumberRange.new(10, 25) -- Starts at the 10 second mark and ends on the 25 second mark once looped.
local playbackRegion = NumberRange.new(5, 25) -- Starts at the 5 second mark once the sound starts playing and stops at 25 seconds if the looped property is false.

local newSound = Instance.new("Sound")
newSound.SoundId = "rbxassetid://[Your Sound Id]"
newSound.Looped = true -- Can be used for loop regions.
newSound.PlaybackRegionsEnabled = true -- Be sure to enable this property or the regions won't be able to interact with the sound.
newSound.LoopRegion = loopRegion
newSound.PlaybackRegion = playbackRegion
newSound.Parent = workspace

newSound:Play()
1 Like