Server script not playing sounds after remote event fired

I have been trying to get sounds for my tool to play on the server but I cant seem to figure it out, the client side is outputting remote events but no matter what I try I cant seem to get the server script to respond in any type of way

Here is my local script thats firing the remote events

local toolName = "AK-12" -- or "Glock17"
local tool = game.Players.LocalPlayer.Backpack:FindFirstChild(toolName)
local position = tool and tool.Handle.Position

local muzzleFlashEvent = eventsFolder:WaitForChild("MuzzleFlashEvent")

local function playMuzzleEffect()
	muzzleFlashEvent:FireServer(tool.AK12.Muzzle.Part.Position)
end

local function fireFiringSound(toolName)
    local position = tool.AK12.Muzzle.Part.Position -- Adjust this line according to your tool structure
    ReplicatedStorage.Events.FiringSoundEvent:FireServer(toolName, game.Players.LocalPlayer, position)
    print("Local Firing input detected")
end

local function fireReloadSound(toolName)
    local position = tool.Handle.Position -- Adjust this line according to your tool structure
    ReplicatedStorage.Events.ReloadSoundEvent:FireServer(toolName, game.Players.LocalPlayer, position)
    print("Local Reloading input detected")
end

local function fireClickSound(toolName)
    local position = tool.Handle.Position -- Adjust this line according to your tool structure
    ReplicatedStorage.Events.ClickSoundEvent:FireServer(toolName, game.Players.LocalPlayer, position)
    print("Local Clicking input detected")
end

and heres the server script that should be receiving them

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local SoundService = game:GetService("SoundService")

print("Server input detected")

-- Function to play AK-12 sounds
local function playAK12Sounds(parent, position)
	local soundFolder = ReplicatedStorage.Sounds.AK12
	local fireRate = 0.3 -- Time between shots in seconds
	local boltcomboPlaybackSpeed = 0.5 / fireRate
	local gunshotPlaybackSpeed = 0.5 / fireRate

	if not soundFolder then
		warn("Sound folder for AK-12 not found.")
		return
	else
		print("Server AK12 detected")
	end

	local function playReload()
		print("Server ReloadSounds loaded")
		local sounds = {}
		task.wait(0.37)
		sounds.bolt_out = soundFolder:FindFirstChild("bolt_out")
		task.wait(0.40)
		sounds.mag_out = soundFolder:FindFirstChild("mag_out")
		task.wait(0.44)
		sounds.pocket = soundFolder:FindFirstChild("pocket")
		task.wait(0.37)
		sounds.mag_in = soundFolder:FindFirstChild("mag_in")
		return sounds
	end

	local function playFiring()
		print("Server Firing loaded")
		local sounds = {}
		task.wait(0.07)
		sounds.gunshot = soundFolder:FindFirstChild("gunshot")
		sounds.boltcombo = soundFolder:FindFirstChild("boltcombo")
		if sounds.boltcombo and sounds.gunshot then
			sounds.boltcombo.PlaybackSpeed = boltcomboPlaybackSpeed
			sounds.boltcombo.Looped = true
			sounds.gunshot.Looped = true
		end
		return sounds
	end

	local function playClick()
		print("Server Clicking loaded")
		local sounds = {}
		task.wait(0.07)
		sounds.boltcombo = soundFolder:FindFirstChild("boltcombo")
		return sounds
	end

	return { playReload(), playFiring(), playClick(), position = position }
end

-- Function to play Glock17 sounds
local function playGlock17Sounds(parent, position)
	local soundFolder = ReplicatedStorage.Sounds.Glock17
	if not soundFolder then
		warn("Sound folder for Glock17 not found.")
		return
	end

	local function playReload()
		local sounds = {}
		sounds.mag_in = soundFolder:FindFirstChild("mag_in")
		sounds.mag_out = soundFolder:FindFirstChild("mag_out")
		return sounds
	end

	local function playFiring()
		local sounds = {}
		sounds.gunshot = soundFolder:FindFirstChild("gunshot")
		return sounds
	end

	local function playClick()
		local sounds = {}
		sounds.bolt_in = soundFolder:FindFirstChild("bolt_in")
		sounds.bolt_out = soundFolder:FindFirstChild("bolt_out")
		return sounds
	end

	return { playReload(), playFiring(), playClick(), position = position }
end

-- Function to play sounds based on the tool name
local function playToolSounds(toolName, parent, position)
	if toolName == "AK-12" then
		print("Server AK12 Sounds loaded")
		return playAK12Sounds(parent, position)
	elseif toolName == "Glock17" then
		return playGlock17Sounds(parent, position)
	else
		warn("No sound function found for "..toolName)
		return
	end
end

-- Function to play sounds from a table
local function playSounds(sounds, parent, position)
	local soundInstances = {}
	for i, sound in pairs(sounds) do
		if sound then
			local soundInstance = sound:Clone()
			soundInstance.Parent = parent
			soundInstance.Position = position
			soundInstance:Play()
			table.insert(soundInstances, soundInstance)
			print("Server Sounds Played")
		end
	end
	return soundInstances
end

-- Function to stop sounds
local function stopSounds(soundInstances)
	for _, soundInstance in pairs(soundInstances) do
		soundInstance:Stop()
		soundInstance:Destroy()
		print("Server Sounds Stopped")
	end
end

-- Remote function to handle firing sounds
local function fireSound(toolName, player)
	local character = player.Character
	if not character then return end

	local tool = character:FindFirstChild(toolName)
	if not tool then return end

	local position = tool.Handle.Position
	local firingSounds = playToolSounds(toolName, tool, position)

	local soundInstances = playSounds(firingSounds, SoundService, position)

	-- Sound will play for 2 seconds, adjust according to your needs
	task.wait(2)

	-- Stop and destroy the sounds
	stopSounds(soundInstances)
	print("Server Fire Sounds Fired")
end

-- Remote function to handle reload sounds
local function reloadSound(toolName, player)
	local character = player.Character
	if not character then return end

	local tool = character:FindFirstChild(toolName)
	if not tool then return end

	local position = tool.Handle.Position
	local reloadSounds = playToolSounds(toolName, tool, position)

	local soundInstances = playSounds(reloadSounds, SoundService, position)

	-- Sound will play for 2 seconds, adjust according to your needs
	task.wait(2)

	-- Stop and destroy the sounds
	stopSounds(soundInstances)
	print("Server Reload Sounds Fired")
end

-- Remote function to handle clicking sounds
local function clickSound(toolName, player)
	local character = player.Character
	if not character then return end

	local tool = character:FindFirstChild(toolName)
	if not tool then return end

	local position = tool.Handle.Position
	local clickSounds = playToolSounds(toolName, tool, position)

	local soundInstances = playSounds(clickSounds, SoundService, position)

	-- Sound will play for 2 seconds, adjust according to your needs
	task.wait(2)

	-- Stop and destroy the sounds
	stopSounds(soundInstances)
	print("Server Reload Sounds Fired")
end

-- Event to play sounds when the FiringSoundEvent is fired
ReplicatedStorage.Events.FiringSoundEvent.OnServerEvent:Connect(fireSound)

-- Event to play sounds when the ReloadSoundEvent is fired
ReplicatedStorage.Events.ReloadSoundEvent.OnServerEvent:Connect(reloadSound)

-- Event to play sounds when the ClickSoundEvent is fired
ReplicatedStorage.Events.ClickSoundEvent.OnServerEvent:Connect(clickSound)

for this script just ignore the glock17 part as im only testing with the AK12, im trying to have a setup where I have the script grab the sounds and play them in order based on the guns name and folder name, Im currently getting no Syntax issues and I’ve made sure that everything is named correctly

3 Likes

image
Might not be playing if some of there are nil.

2 Likes

Yeah after removing the checks it seems like its the tool part since it can’t find the path to the tool and is saying “attempt to index nil with FindFirstChild”

2 Likes

Check if the player variable is nil. Try putting a print(player) inside your function as the very first line. The error typically means the variable you’re using FindFirstChild() on is nil.

1 Like

thanks for the tip I now see that the toolname and player were accidentally swapped around, now it seems to be going through but a new issue is happening at line 108

local soundInstance = sound:Clone()

inside of the playSounds function is having the issue
attempt to call missing method ‘Clone’ of table

In the i, v loop, just putting pairs(sounds) doesn’t get anything at all. Try using pairs(sounds:GetChildren()) to get every thing inside of it.

it seems to be the same issue its just marking “GetChildren” as the missing method instead of clone

Oh no, I mean in this line to use GetChildren(). That way, you get everything inside of the table, instead of just the table:

for i, sound in pairs(sounds:GetChildren()) do

Keep the :Clone() there by the way.

Heres the code this is what its giving me the issue for “GetChildren” did I put it in the wrong spot?

local function playSounds(sounds, parent, position)
	local soundInstances = {}
	for i, sound in pairs(sounds:GetChildren()) do
		if sound then
			local soundInstance = sound:Clone()
			soundInstance.Parent = parent
			soundInstance.Position = position
			soundInstance:Play()
			table.insert(soundInstances, soundInstance)
			print("Server Sounds Played")
		end
	end
	return soundInstances
end

Check if sound is a table. I’m not sure if they allow tables to be cloned.

yup I just checked sounds is a table, but I need it since it holds all of the sounds n stuff what would be a better way to do this then?

I found table.clone for a shallow copy of a table but it doesnt copy sub tables, would that work or should I try some form of deep table clone?

I now have this setup and its getting through to the soundInstance but now it can’t do the soundInstance:Play() because its missing from the table

local function deepCopy(original)
	local copy = {}
	for k, v in pairs(original) do
		if type(v) == "table" then
			v = deepCopy(v)
		end
		copy[k] = v
	end
	return copy
end

-- Function to play sounds from a table
local function playSounds(sounds, parent, position)
	local soundInstances = {}
	if type(sounds) == "table" then
		print (sounds)
		for i, sound in pairs(sounds) do
			if sound then
				local soundInstance = deepCopy(sounds)
				soundInstance.Parent = parent
				soundInstance.Position = position
				soundInstance:Play()
				table.insert(soundInstances, soundInstance)
				print("Server Sounds Played")
			end
		end
	else
		print("Sounds is not a table")
	end
	return soundInstances
end

Here is the latest script, the only issues left are playing the sounds in the table and then stopping them the closest I’ve been able to find to playing sounds on/in a table is sounds[( #sounds)]:Play() but even that doesn’t seem to work as it keeps saying Play() is not defined in the table

local function deepCopy(original)
	local copy = {}
	for k, v in pairs(original) do
		if type(v) == "table" then
			v = deepCopy(v)
		end
		copy[k] = v
	end
	return copy
end

-- Function to play sounds from a table
local function playSounds(sounds, parent, position)
	local soundInstances = {}
	if type(sounds) == "table" then
		for i, sound in pairs(sounds) do
			if sound then
				local soundInstance = deepCopy(sound)
				soundInstance.Parent = parent
				soundInstance.Position = position
				-- Some way to play the sounds in the table
				table.insert(soundInstances, soundInstance)
				print("Server Sound Played")
			end
		end
	else
		print("Sounds is not a table")
	end
	return soundInstances
end

-- Function to stop sounds
local function stopSounds(soundInstances)
	for _, soundInstance in pairs(soundInstances) do
		-- Some way to stop the sounds in the table
		soundInstance:Destroy()
		print("Server Sounds Stopped")
	end
end

make it create an actual sound instance?
local copy = Instance.new(“Sound”)
Obviously :Play() wont work if the copy its returning is a table rather than an Instance.

1 Like

Is it even printing something? If not, try parenting your server script to serverscriptservice, remoteevent to replicatedstorage and local to the tool.

Appreciate all the suggestions you all helped me in figuring out this is way to complex of a way to do this lol, I ended up switching to using a script and remote event inside the gun which is 10x easier and means I dont have to make it work for every gun and can just run for its current gun and grab sounds from the handle

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