Sound doesnt play

Greetings delevopers! I’ve encountered a weird problem where the sound doesnt play in my script.
So basically i wanted to make a sound-fade script, if you reached x studs to npc, itll play sound. I made a script that wont work so i wanted to ask for help. Thank you!

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

local function getNPC()
	return workspace:FindFirstChild("EnemyNPC")
end

RunService.RenderStepped:Connect(function()
	local npc = getNPC()
	if not npc then return end

	local npcRoot = npc:FindFirstChild("HumanoidRootPart")
	local sound = npc:FindFirstChild("Tung Tung Tung Sahur")
	if not (npcRoot and sound) then return end

	local playerRoot = character:FindFirstChild("HumanoidRootPart")
	if not playerRoot then return end

	local distance = (playerRoot.Position - npcRoot.Position).Magnitude

	if distance <= 20 then
		if not sound.IsPlaying then
			sound:Play()
		end
	else
		if sound.IsPlaying then
			sound:Stop()
		end
	end
end)
3 Likes

This might be why. You blocked your sound card’s micro-transistors that wa-

Jokes aside. It depends:

  • Do you want the sound to be emitted from the character (so you know where is it from)? Try putting the sound in a part of the character and change RollOffMaxDistance to 20 and, if you want the volume to be the same all the time, RollOffMinDistance to 20 too.

  • If you want to do exactly what your scripted is supposed to do, I need more information. What happens when you play the sound manually? Did you try putting print("it should play") before the sound plays? This can be very useful.

Good luck!

1 Like

Sound is already in character. Nothing prints at all, changed rolls as you said, nothing happened, quite confusing. Maybe this occurs because im trying to do this manipulations in local script, or it doesnt rlly matter?

Edit: I also tried playing it manually, sound just plays without scripting :no_mouth:

Can you try printing something for every return line and says what’s the result?
Also you said it is a LocalScript, where is it located?

1 Like

It’s located in npc itself. Yeah i will look into it

1 Like

This. My question with “playing manually” was to determine whether the sound is in measure to be played. It is, which means that the script is indeed the problem. In your scenario, it’s the fact it’s a localscript in a character.

A LocalScript runs for a player. In a NPC, it won’t know who is the player compared to a tool, a PlayerScript or something else. You need to use this LocalScript in PlayerScripts (put in StarterPlayer → StarterPlayerScripts) and use a workaround to find the NPC.

For that, I advise you to put a line in the server script (if there’s one?) that will assign a value or something that will allow to the local script to find it in ease. If you want to go simpler, you can also simply find it from the local script (but if there are multiple NPCs that would be problematic.

EDIT: You likely have nothing to do else because you already find the NPC so. So just put in in StarterPlayerScripts and you should be good!

2 Likes

Looks good logic wise… Make sure it’s a LocalScript and may as well drop it in StarterCharacterScripts,
because the script depends on the player’s character existing. Test the sound in a temp script to be sure it’s working.

1 Like

The only problem is that the script doesn’t need to call CharacterAdded in that scenario. If you can, it is advised to try using PlayerScripts as they don’t have to reload each time, which is better for performance (uhm, from a little bit, but you see what I mean).

Not only that, but if the player dies, the sound will restart when they respawn since it makes a new script. But whatsoever, it’s up to OP to know what’s best for them.

1 Like

This line covers that too.. if it’s found, it isn’t waiting()
Wouldn’t hurt to add a pause right after that line..

SoftlockedAt15 I see, and ya.
StarterCharacterScripts, StarterPlayerScripts or StarterGui.

Maybe check with prints in a lot of that.. all silent fails.
TungTungTungSahur --don’t like spaces

1 Like

Thank you so much, it actually worked. I’ll know this next time! :slight_smile:

1 Like

doesn’t really matter to me cause i lack skills in scripting in order to make a game. I’m just learning right so thats cool for me anyways and i like the result :smiley:

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