-
What do you want to achieve? A footsteps sound system with no delays, if possible. (This is done on a client script)
-
What is the issue? There’s a delay when playing the sounds, or the sounds just don’t play at all.
-
What solutions have you tried so far? I tried using soundIds, got the same result. Also tried using the
ContentProvider
, and no luck.
Everything is done on a localscript/clientscript, the sounds are located in ReplicatedStorage, i tried preloading the sounds, using soundIds, but nothing seemed to fix this issue. I printed some of the sound’s properties and the IsLoaded
property always returns true, TimeLength
always returns something higher than 0.
This is the Footsteps
script, it plays based on certain conditions. Now I did check if the conditions affected caused the delay, but they didnt.
-- First-class functions
local function getPitch() : number
return math.random(footstepConstants.minSpeed, footstepConstants.maxSpeed)
end
local function playSound(sound : Sound)
print(sound.TimeLength, sound.IsLoaded, sound.Playing)
debris:AddItem(sound, sound.TimeLength)
sound:Play()
end
local function canPlay(sound : Sound, moveDirection : Vector3, tickArray : tickArray) : boolean
local currentTime = tick()
local magnitudeDirection = moveDirection.Magnitude
local canPassCooldown = (currentTime - tickArray.lastTick > footstepConstants.cooldown)
local condition = (sound and magnitudeDirection > 0 and canPassCooldown)
tickArray.lastTick = condition and currentTime or tickArray.lastTick
return condition
end
local function playFootstep(sound : Sound, soundID : string, parent : BasePart, pitch : number, isPlayable : boolean)
if (not isPlayable) then
return
end
sound.Parent = parent
sound.PlaybackSpeed = pitch
sound.Volume = footstepConstants.generalVolume
sound.SoundId = soundID
playSound(sound)
end
local function getSound(material : Enum.Material) : Sound
local soundTable = footsteps:GetTableFromMaterial(material)
return footsteps:GetRandomSound(soundTable)
end
local function createClass()
local footstepsClass = {}
footstepsClass.execute = function(character : utilities.character, functionParameters : {utilities.steppedFunction})
local humanoid = character.Humanoid
local rootPart = humanoid.RootPart
local tickArray = {lastTick = 0} -- wrapped in a table, since items in tables update regardless of scope
local function stepped()
local material = humanoid.FloorMaterial
local moveDirection = humanoid.MoveDirection
local pitch, sound, soundId = getPitch(), getSound(material)
local condition = canPlay(sound, moveDirection, tickArray)
task.synchronize()
playFootstep(sound, soundId, rootPart, pitch, condition)
end
utilities.steppedWrap(functionParameters, stepped, script.Name, function() return not character end, true)
end
return footstepsClass
end
And this is my footsteps module, which returns a sound instance with a randomly picked ID based on the humanoid’s FloorMaterial
-- First-class functions
local sounds = {
[Enum.Material.SmoothPlastic] = script.Concrete,
[Enum.Material.Plastic] = script.Concrete,
[Enum.Material.Granite] = script.Concrete,
[Enum.Material.Concrete] = script.Concrete,
[Enum.Material.Slate] = script.Concrete,
[Enum.Material.Pebble] = script.Concrete,
[Enum.Material.Rock] = script.Concrete,
[Enum.Material.Sand] = script.Sand,
[Enum.Material.Snow] = script.Sand,
[Enum.Material.Wood] = script.Wood,
[Enum.Material.WoodPlanks] = script.Wood
}
local function getRandomSound(materialType : Enum.Material) : Sound
local soundSource = materialType ~= Enum.Material.Air and sounds[materialType]:GetChildren()
return soundSource and soundSource[math.random(1, #soundSource)]
end
return getRandomSound