Hello devs!
Im having a issue with sound playing for a simple enemies run animation. What I am trying to get is footstep sounds to play every time the run animations feet touch the ground (AnimationEvent’s are reached in the animation)
However for whatever reason, the sounds play but occasionally a sound or 2 wont play, so it will step 4 times but then miss a couple before continuing.
I have tried sound pooling, not removing the sounds and added prints to see if the markers reaching every time, which it IS every single time it passes the marker, it’s just the sounds not keeping up.
Ive provided an example script on how ive written it in the other script.
--This is an example script of how my other script does it
--//Npc based
local NPC = script.Parent
local Humanoid = NPC:WaitForChild("Humanoid")
local Animator = Humanoid:WaitForChild("Animator")
--//Sound based
local stepSoundIDs_Walk = {
"rbxassetid://6563561285",
"rbxassetid://6563561749",
"rbxassetid://6563562192"
}
--//Animations
local runAnimation = NPC:WaitForChild("Animations").Chase
local runTrack = Animator:LoadAnimation(runAnimation)
runTrack:Play()
--//Functions
local function randomFootstep()
local soundIndex = math.random(1, #stepSoundIDs_Walk)
local footstepSound = Instance.new("Sound") --Better ways of doing this without lag but whatevs
footstepSound.SoundId = stepSoundIDs_Walk[soundIndex]
footstepSound.Parent = NPC:WaitForChild("HumanoidRootPart")
footstepSound:Play()
--Clear up the sound once finished
footstepSound.Ended:Connect(function()
footstepSound:Destroy()
end)
end
--//Markers
runTrack:GetMarkerReachedSignal("Step"):Connect(function()
task.spawn(function()
randomFootstep()
end)
end)
Im not sure if i am doing my logic incorrectly or not so any help and feedback would be great ^^