Hey, developers! I want to make something where when the player takes a step (relative to their running animation), a sound plays along with it changing the SoundId after every step for a more realistic feel. The code I’ve tried doesn’t seem to work where the sound doesn’t play at all and the SoundId doesn’t change. Any solutions?
--//variables
local RunService = game:GetService("RunService")
local Character = script.Parent.Parent
local Humanoid = Character:WaitForChild("Humanoid")
local RootPart = Character:WaitForChild("HumanoidRootPart")
local WalkSound = RootPart:WaitForChild("WalkSound")
local Check
local currentStep = 0
local isRunning = false
local footstepSfx = {
9332007748,
9332017360,
9332019868,
9332021736,
9332023027,
9332024749
}
--//functions
local function footstep()
print("Took a step!")
currentStep += 1
if currentStep == 7 then
currentStep = 1
end
RootPart.WalkSound.SoundId = "rbxassetid://" .. tostring(footstepSfx[currentStep])
end
Check = Humanoid.Running:Connect(function(speed)
local function onMarkerReached()
WalkSound:Play()
footstep()
end
if speed > 0 and isRunning == false then
isRunning = true
if not Character:FindFirstChild("Humanoid") or Humanoid.Health == 0 then
Check:Disconnect()
end
for _, anim in pairs(Humanoid:GetPlayingAnimationTracks()) do
local track
if anim.Animation.Parent and anim.Animation.Parent:IsA("StringValue") then
track = anim.Animation.Parent.Name
else
track = anim.Animation.Name
end
if track ~= nil then
if track == "run" then
isRunning = true
anim:GetMarkerReachedSignal("Stepped"):Connect(onMarkerReached)
anim:GetMarkerReachedSignal("Stepped1"):Connect(onMarkerReached)
end
end
end
elseif speed <= 0 then
isRunning = false
end
end)