Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago

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)
0
Since its a localscript, only the client hears the sound. Consider RemoteEvents MakeYourEscape 334 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago
-- 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

Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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.

0
I used UserInputService and I have an error. "(name of sound) is not a valid member of LocalScript" Manuelolol 22 — 5y
0
and I have the same error with the first metod Manuelolol 22 — 5y

Answer this question