I am trying to get a sound to play when the player presses a key e.g. ‘K’ or maybe ‘J’.
I have been trying a lot but I still cannot get it to work. (I am not much of a scripter)
Here is the script so far:
It would be greatly appreciated if you could help me.
Only the client can detect input from the player. The server can’t. Make it a local script. Also shove the script into starterplayer > starterplayerscripts. If you want everyone to hear it then put the sound in like workspace and then play it on the server using a remote event / function if you need so everyone can hear it.
Use RemoteEvents if you want to play the sound from the client to the server, and have the Sound inside the workspace to play it so that everyone can hear it, I’ll give an example:
--Client
local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent")
local UIS = game:GetService("UserInputService")
UIS.InputBegan:Connect(function(Key, GameDetect)
if not GameDetect then return end --This will detect any non-ingame Inputs
if Key.KeyCode == Enum.KeyCode.K then
Event:FireServer()
end
end)
--Server
local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent")
local Sound = workspace.Sound
Event.OnServerEvent:Connect(function()
Sound:Play()
end)