Sound is glitching when changing time position

Im trying to synchronize train sound with speed, but its very glitchy, help please

-- Script for synchronizing sound time position with train speed
local twen = game:GetService("TweenService")
local train = script.Parent  -- Assuming this script is a child of the train
local sound = train.Depart

while true do
	-- Get the current speed of the train
	local speed = train.Velocity.Magnitude

	-- Map the speed to a range of sound time positions
	local minSpeed = 0
	local maxSpeed = 90
	local minTime = 0
	local maxTime = sound.TimeLength
	local timea = math.clamp((speed - minSpeed) / (maxSpeed - minSpeed) * (maxTime - minTime) + minTime, minTime, maxTime)

	-- Set the sound time position
	twen:Create(sound,,{TimePosition = math.round(timea)}):Play()
1 Like

Time is moving linearly, and you are attempting to change the time position on a curve. This is likely causing problems, since the audio is playing at a different speed than the time position is increasing.

Why are you using TweenService with TimePosition?

Is this trying to synchronize the piston with the sound of the piston, or are you just making a sound faster when the train moves faster?

I’ve done a similar thing with a single cylinder steam engine driving a boat in my Steampunk place. I just scripted the change of the Sound.PlaybackSpeed to match the piston speed.

No, I’m trying to synchronize the sound in this way: so that the sound plays until the moment when the train reaches its maximum speed, that is, stretching the sound, speed is not a suitable method. But at the same time, part of the sound that in real life sounds, for example, at 10 km/h, should also sound in Roblox at a similar speed, and so on with all speeds from 0 to 90 km/h

Tried to make the transition between time positions smoother