Sound not playing

I want to play a death sound whenever a mob dies in my game.

newMob.Humanoid.Died:Connect(function()


				if newMob.Name == "fastboi" then
					ServerStorage.Sounds:FindFirstChild(newMob.Name):Play()
				elseif newMob.Name == "fein" then
					ServerStorage.Sounds:FindFirstChild(newMob.Name):Play()
				elseif newMob.Name == "megafein" then
					ServerStorage.Sounds:FindFirstChild(newMob.Name):Play()
				end
end)

This is in a module script, could that have something to do with it not working?

1 Like

Yes, the module script is significant for some reasons.

  1. It must be manually “activated” by another script to run.
require(game.ServerScriptService.moduleScript)
  1. You need to have a return value
-- In a module script
local module = {}

-- Code here

return module -- Must return *exactly* one table, function, number, string or any non-nil value

I would change it to a server script unless you have a specific reason for it to be a module script.

I do actually have a lot of other code in the module script, as its the script that completely handles the mob spawning and scaling throughout the game.

Along with that there is more to the died function

           newMob.Humanoid.Died:Connect(function()


				if newMob.Name == "fastboi" then
					ServerStorage.Sounds:FindFirstChild(newMob.Name):Play()
				elseif newMob.Name == "fein" then
					ServerStorage.Sounds:FindFirstChild(newMob.Name):Play()
				elseif newMob.Name == "megafein" then
					ServerStorage.Sounds:FindFirstChild(newMob.Name):Play()
				end

				newMob:Destroy()

				local player = map.Player1.Value.Value
				local money = game.Players:FindFirstChild(player).Dosh
				local round = workspace.GameValues.Round

				money.Value += 5*round.Value
			end)

Would I just have to make a bindable function and connect it to a server script then?

Full Mob Module if it matters:

Summary
local PhysicsService = game:GetService("PhysicsService")
local ServerStorage = game:GetService("ServerStorage")

local bindables = ServerStorage:WaitForChild("Bindables")
local updateBaseHealthEvent = bindables:WaitForChild("UpdateBaseHealth")

local mob = {}

local round = workspace:WaitForChild("GameValues"):WaitForChild("Round")

function mob.Move(mob, map)
	local humanoid = mob:WaitForChild("Humanoid")
	local waypoints = map.Player1.PlayerPath.Waypoints

	for waypoint=1, #waypoints:GetChildren() do
		mob.MovingTo.Value = waypoint
		humanoid:MoveTo(waypoints[waypoint].Position)
		humanoid.MoveToFinished:Wait()

	end
	mob:Destroy()
	updateBaseHealthEvent:Fire(1)
end
function mob.Spawn(name, quantity, map)
	local mobExists = ServerStorage.Mobs:FindFirstChild(name)

	if mobExists then
		for i=1, quantity do
			task.wait(.75)
			local newMob = mobExists:Clone()
			newMob.Humanoid.MaxHealth = newMob.Humanoid.MaxHealth*round.Value
			newMob.Humanoid.Health = newMob.Humanoid.MaxHealth
			newMob.HumanoidRootPart.CFrame = map.Spawner.CFrame
			newMob.Parent = workspace.MobRealm
			newMob.HumanoidRootPart:SetNetworkOwner(nil)

			local movingTo = Instance.new("IntValue")
			movingTo.Name = "MovingTo"
			movingTo.Parent = newMob

			for i, object in ipairs(newMob:GetDescendants()) do
				if object:IsA("BasePart") then
					object.CollisionGroup = "Mob"
				end
			end

			newMob.Humanoid.Died:Connect(function()


				if newMob.Name == "fastboi" then
					ServerStorage.Sounds:FindFirstChild(newMob.Name):Play()
				elseif newMob.Name == "fein" then
					ServerStorage.Sounds:FindFirstChild(newMob.Name):Play()
				elseif newMob.Name == "megafein" then
					ServerStorage.Sounds:FindFirstChild(newMob.Name):Play()
				end

				newMob:Destroy()

				local player = map.Player1.Value.Value
				local money = game.Players:FindFirstChild(player).Dosh
				local round = workspace.GameValues.Round

				money.Value += 5*round.Value
			end)

			coroutine.wrap(mob.Move)(newMob, map)
		end
	else
		warn("Mob does not exist:", name)
	end
end
return mob

Is the script being required? And if so, is it from a local script or a server script. If you’re using a module script, that’s fine, it just needs to be setup right.

Yeah it’s being required in a server script with the rest of my modules;

local base = require(script.Base)
local mob = require(script.Mob)                --this one
local tower = require(script.Tower)
local upgrade = require(script.Upgrade)
local sell = require(script.Sell)
local map = workspace.Board

Oh, sorry! You might need to parent the sounds to Workspace or SoundService before playing them.

The client doesn’t see ServerScriptService or ServerStorage so it won’t be able to “know” when they play.

There’s probably a more practical way to do this, but something like this could work.

local function playSoundForPlayers(sound)
	sound = sound:Clone()
	sound.Parent = game:GetService("SoundService")
	sound:Play()
	sound.Ended:Connect(function()
		sound:Destroy()
	end
end

-- Remove redundancy
if table.find({"fastboi", "fein", "megafein"}, newMob.Name) then
	playSoundforPlayers(ServerStorage.Sounds:FindFirstChild(newMob.Name))
end