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
local sound = script.Sound
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
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
local uis = game:GetService("UserInputService")
local plr = game.Players.LocalPlayer
local enabled = false
local tool = script.Parent
local sound = scirpt.Sound
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.
1 comment
Since its a localscript, only the client hears the sound. Consider RemoteEvents