SoundStepEvents

Introducing SoundStepEvents

Much like the roblox animator which has custom keyframe events, I wanted to implement something like it for roblox sounds, now this is a useful module for many things such as play orientated features which are activated depending on the time position of a sound.

While there isnt an official remedy for doing this, this module can mimic that in a sort of way.
There are known limitations with this module, such as, you cannot use decimal numbers such as 5.2 as the script floors the time position to find an entire number.

Now, I whipped this up in a few minutes as I needed it for one of my own games, but feel free to use it, I made some speedy documentation to get you started.

If there are bugs, I’ll not be fixing it as this is just a quick module to give developers a quick remedy to this annoying lack in feature.

local SoundStepEvents = require(location.to.class)

local Stepper = SoundStepEvents.new({
	Sound = workspace["Sound"], -- The sound you wish to bind to.
	Events = {
		[5] = "This event fires when time position floors itself to 5 seconds!",
		[10] = "This event fires when time position floors itself to 10 seconds!"
		--Format is [timeposition in whole number] = "event name"
	},
	Misc = {
		RunOnce = false; --Destroy instance once the sound finishes.
	}
})

Stepper.EventStepped:Connect(function(EventName: string) --Connect an event to listen for when the listed events are fired.
	print(EventName) --Collect the event.
end)

Stepper:GetStepsReached() --If not destroyed and sound is playing, collect all markers reached.

Stepper:Destroy() --Destroy resource usage.

Once you connect your sound instance to the stepper module, it will automatically start/stop when you use :Play() or :Stop() on your sound instance.

Get it here :slight_smile:

14 Likes

Awesome module! I hope Roblox makes something likes this official. I can’t look at the module’s code, so may I ask why the numbers are rounded?

1 Like

Sure.

Because roblox sound instances play at a heavy number of decimal places, unless you have the exact number down to the 10th digit such as 1.023226429 then the event wont hit.

I use math.floor to round it back to the nearest entire digit, this is so it gives you more accurate results for your events.

2 Likes

While this is more accurate to the second, what if we want the event to trigger in between a second? My suggestion is to change it to miliseconds, and then don’t check if the value hits exactly, but if the value reached above it.
Something like this could work, maybe?

		local FlooredPosition = math.floor(self.Sound.TimePosition * 1000)
		
		for time_position, eventTime in pairs(self.Events) do
			if FlooredPosition >= time_position and not self.Temporary.Debounces[eventTime] then
				self.Temporary.Debounces[eventTime] = true;
				self.EventStepped:Fire(eventTime)
			end
		end```
3 Likes

I don’t like the fact that it is limited to flooring to one second. You should try to provide options:

  • Connect to when the event will probably fire at the next frame.
  • Connect to when the event has fired in the current frame.