Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I make my tool play sounds and make all players hear them?

I made a tool that plays a sound when you click a key in your keyboard (Q or E) but players cant hear the sounds. How do I fix that? the script is this (its a local script):

player = game.Players.LocalPlayer

tool = script.Parent

tool.Equipped:connect(function(mouse)
	
	mouse.KeyDown:connect(function(key)
		
		if key == "q" then
			
			wait (0)
			
            script.Sound1:Play()
            
		end
		
		if key == "e" then
			
			wait (0)
			
            script.Sound2:Play()
			
		end
		
	end)
	
end)

1 comment

0

Since its a localscript, only the client hears the sound. Consider RemoteEvents

2 answers

✓ Accepted answer
-- Server Script Named "server":
local remote = Instance.new("RemoteEvent")
remote.Name = "PlaySound"
remote.Parent = script

remote.OnServerEvent:Connect(function(player, sound)
if sound.ClassName == "Sound" then
sound:Play()
end
end)
-- Server Script END

-- LocalScript: 
player = game.Players.LocalPlayer

tool = script.Parent

tool.Equipped:connect(function(mouse)

    mouse.KeyDown:connect(function(key)

        if key == "q" then

            wait (0)

           script.Parent.server.PlaySound:FireServer(script.Sound1)

        end

        if key == "e" then

            wait (0)

script.Parent.server.PlaySound:FireServer(script.Sound2)

        end

    end)

end)
-- LocalScript END

Since this is in a local script, it is required to use a remote event. Insert a remote event in the replicated storage and name it whatever.

Then copy this: LOCAL SCRIPT

local FE = game.ReplicatedStorage.RemoteEvent --Name of the remote event
local sound = script.Sound --Or whatever name you gave it

tool.Equipped:Connect(function(mouse)
	mouse.KeyDown:Connect(function(key)
		if key == "q" or key == "e" then
			FE:FireServer(sound)
		end
	end)
end)

SERVER SCRIPT

local FE = game.ReplicatedStorage.RemoteEvent --name of the remote event

FE.OnServerEvent:Connect(function(player,sound)
	sound:Play()
end)

Though, I would recommend that you switch to UserInputService since it is simpler to use. If you decide to use UserInputService then copy this code:

LOCAL SCRIPT V2

local FE = game.ReplicatedStorage.RemoteEvent --name of the remote event
local uis = game:GetService("UserInputService")
local plr = game.Players.LocalPlayer
local enabled = false
local tool = script.Parent
local sound = scirpt.Sound --Or whatever name you gave it

tool.Equipped:Connect(function()
	enabled = true
end)

tool.Unequipped:Connect(function()
	enabled = false
end)


uis.InputBegan:Connect(function(key,chatting)
	if key.KeyCode == Enum.KeyCode.E or key.KeyCode == Enum.KeyCode.Q and chatting == false and enabled == true then
		FE:FireServer(sound)
	end
end)

There are many solutions to this, but I think this will help you.

2 comments

0

I used UserInputService and I have an error. "(name of sound) is not a valid member of LocalScript"

0

and I have the same error with the first metod

This discussion is preserved as it was. New answers, comments, and votes are closed.