Play walk sound on every step

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)
1 Like

You cannot create a function inside a function, and even if you can, you are just creating a function, and not calling it. Besides, whats the point of creating a function if you just got 2 lines in it?

Yes you can…

If you want to call the function multiple times in different spots, not in a loop, (disregarding how he doesn’t call it) that’s what it could be used for.

In C# you definitely can not, i haven’t tried it in Lua but i dont think its any different, have you tried or are you just assuming?

I know what a function can be used for, but he doesnt use it that way, does he? So putting it out of function would solve his problem

I’ve indeed tried.
charrrrrrrrrrrr

I made a function with only 2 lines because when I connected a new function to the GetMarkerReachedSignal event, it didn’t run, so I tried making a function then connecting that function to the event.