NPC Footsteps not working from afar

local npc = script.Parent -- Assuming the script is inside the NPC
local humanoid = npc:FindFirstChild("Humanoid")

if not humanoid then
	warn("Humanoid not found!")
	return
end

-- Footstep sound settings
local footstepSoundId = "rbxassetid://198360378" -- Replace with your footstep sound asset ID
local footstepSound = Instance.new("Sound")
footstepSound.SoundId = footstepSoundId
footstepSound.Volume = 1 -- Set the volume as you like
footstepSound.Parent = npc.HumanoidRootPart -- Attach sound to HumanoidRootPart

local isMoving = false
local lastPlayedTime = 0
local footstepInterval = 0.4 -- Time in seconds between footsteps (you can adjust this)

-- Function to handle the NPC running state
humanoid.Running:Connect(function(speed)
	if speed > 0 then
		local currentTime = tick()
		if currentTime - lastPlayedTime > footstepInterval then
			-- Play the footstep sound if it's not already playing
			if not footstepSound.IsPlaying then
				footstepSound:Play()
			end
			lastPlayedTime = currentTime
		end
		isMoving = true
	else
		-- Stop the currently playing sound if NPC stops moving
		if footstepSound.IsPlaying then
			footstepSound:Stop()
		end
		isMoving = false
	end
end)

The audio wouldnt play all the time. Like the audio would start playing once when I start the game, stops working, and then starts working again when it touches the player. Idk if its because the audio didnt load or smth but idk what to do abt this anymore.

3 Likes

Quick question. What is the purpose of you setting the debounce isMoving = true and isMoving = false? It looks like no part of your script depends on the debounce. Do you want to put a
if isMoving then return end at the start? Something like this:

local npc = script.Parent -- Assuming the script is inside the NPC
local humanoid = npc:WaitForChild("Humanoid")

if not humanoid then
	warn("Humanoid not found!")
	return
end

-- Footstep sound settings
local footstepSoundId = "rbxassetid://198360378" -- Replace with your footstep sound asset ID
local footstepSound = Instance.new("Sound")
footstepSound.SoundId = footstepSoundId
footstepSound.Volume = 1 -- Set the volume as you like
footstepSound.Parent = npc.HumanoidRootPart -- Attach sound to HumanoidRootPart

local isMoving = false
local lastPlayedTime = 0
local footstepInterval = 0.4 -- Time in seconds between footsteps (you can adjust this)

-- Function to handle the NPC running state
humanoid.Running:Connect(function(speed)
    if isMoving then
        return end

    isMoving = true

	if speed > 0 then
        
		local currentTime = tick()
		if currentTime - lastPlayedTime > footstepInterval then
			-- Play the footstep sound if it's not already playing
			if not footstepSound.IsPlaying then
				footstepSound:Play()
			end
			lastPlayedTime = currentTime
		end
	else
		-- Stop the currently playing sound if NPC stops moving
		if footstepSound.IsPlaying then
			footstepSound:Stop()
		end
	end
    isMoving = false
end)
3 Likes

Alright im gonna have to be honest, i used chatgpt. i know how to make some code but like since this was a code i didnt know how to do and i saw no one on youtube doing it, so i used ai. so i basically have no clue why that was used.

2 Likes

I think its ok to use chatgpt, but not if chatgpt gives you code that you yourself have no idea how to manipulate, because chatgpt is wrong oftentime, since it still is only AI and it doesn’t have studio open it front of it.

3 Likes

btw did you write the code correctly cuz the script error is at the “elseif” part and im trying my best to fix it

2 Likes

see edit, change elseif to if.

I also don’t have your NPC model so I can’t directly test it, so see if this works. if it does not, try putting print statements to see where the code breaks

2 Likes

no errors it just dont play the audio at all.

1 Like

I messed around with the debounce again. Check to see if it works. If it does not, then try adding print statements around the important parts and see where the code breaks

robloxapp-20241212-2121269.wmv (2.4 MB)

i cant even explain it and i dont think theres any audio. but when you see the monster touch me thats when it plays the audio only once so i dont know what to do

Hey, I just tested the script with my own player. It works perfectly. I don’t think the error is in the script. It more likely has to do with some other script or the NPC itself. One other change I made to the script, is waiting for the humanoid root part, and setting the sound id to the rootpart this way. This may be another source of the problem. Look at this script, and adapt it to your own.

game:GetService("Players").PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		local humanoid = char:WaitForChild("Humanoid")
		local rootpart = char:WaitForChild("HumanoidRootPart")
		
		if not humanoid then
			warn("Humanoid not found!")
			return
		end

		-- Footstep sound settings
		local footstepSoundId = "rbxassetid://121873091542454" -- Replace with your footstep sound asset ID
		local footstepSound = Instance.new("Sound")
		footstepSound.SoundId = footstepSoundId
		footstepSound.Volume = 1 -- Set the volume as you like
		footstepSound.Parent = rootpart -- Attach sound to HumanoidRootPart

		local lastPlayedTime = 0
		local footstepInterval = 0.4 -- Time in seconds between footsteps (you can adjust this)

		-- Function to handle the NPC running state
		humanoid.Running:Connect(function(speed)
			if speed > 0 then
				local currentTime = tick()
				if currentTime - lastPlayedTime > footstepInterval then
					-- Play the footstep sound if it's not already playing
					if not footstepSound.IsPlaying then
						footstepSound:Play()
					end
					lastPlayedTime = currentTime
				end
			else
				-- Stop the currently playing sound if NPC stops moving
				if footstepSound.IsPlaying then
					footstepSound:Stop()
				end
			end
		end)
	end)
end)
1 Like

well ur script worked for my npc!


		local humanoid = script.Parent:WaitForChild("Humanoid")
		local rootpart = script.Parent:WaitForChild("HumanoidRootPart")

		if not humanoid then
			warn("Humanoid not found!")
			return
		end

		-- Footstep sound settings
        local footstepSoundId = "rbxassetid://99833416016417" -- Replace with your footstep sound asset ID
		local footstepSound = Instance.new("Sound")
		footstepSound.SoundId = footstepSoundId
		footstepSound.Volume = 1 -- Set the volume as you like
		footstepSound.Parent = rootpart -- Attach sound to HumanoidRootPart
		footstepSound.Looped = true

		local lastPlayedTime = 0
		local footstepInterval = 0.2 -- Time in seconds between footsteps (you can adjust this)

		-- Function to handle the NPC running state
		humanoid.Running:Connect(function(speed)
			if speed > 0 then
				local currentTime = tick()
				if currentTime - lastPlayedTime > footstepInterval then
					-- Play the footstep sound if it's not already playing
					if not footstepSound.IsPlaying then
						footstepSound:Play()
					end
					lastPlayedTime = currentTime
				end
			else
				-- Stop the currently playing sound if NPC stops moving
				if footstepSound.IsPlaying then
					footstepSound:Stop()
				end
			end
		end)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.