Add a sound in the player's head

In my game, players have the possibility to push another player by typing “E” everything works fine.
I would like to add a sound to this action so that all players can hear it, I would mainly also like to be able to adjust the distance of the sound with RollOfMaxDistance.

I saw that it might be necessary to add the sound directly in the head of the player when he joins the game to make it work, but I don’t know how to do it, and I don’t know either how to make the sound work when we press “E”.

If someone can explain to me clearly how to do it? (because I’m really new to scripting)Thank you !

here is the Localscript that allows you to execute the action by press E , it is placed in a folder in StarterCharacterScripts:

local Players = game:GetService('Players')
local Player = Players.LocalPlayer  
local Character = Player.Character or Player.CharacterAdded:Wait()
local Camera = workspace.CurrentCamera



local Mouse = Player:GetMouse()
local Limit = 5
local UIS = game:GetService('UserInputService')

local Events = game:GetService('ReplicatedStorage'):WaitForChild('Events')


function GetPlayer(Part) 
	if Part:FindFirstChild("Humanoid") then 
		return Part, true
	end

	if Part:IsA("Accessory") then 
		if Part.Parent:FindFirstChild("Humanoid") then 
			return Part.Parent, true
		end
	end
end



UIS.InputBegan:Connect(function(Key)
	local distance = (Mouse.Hit.p - Player.Character.HumanoidRootPart.Position).Magnitude
	if distance < Limit then
		if Key.KeyCode == Enum.KeyCode.E then
		if Mouse.Target then 
				local PossibleTarget, Has = GetPlayer(Mouse.Target.Parent)

			
			
			if Has then 
					Events.Spells:FireServer(PossibleTarget,'TkPush',Camera.CFrame.LookVector)
			end
		  end
	   end
	end
end)

I also have a moduleScript which regulates the force, the cooldown ect… of the action in ServerScriptService.

1 Like

Expecting every player should hear it I would add a ‘Sound’ to the SoundService with correct sound Id. When the player presses ‘E’ you can fire a remote event in rep.storage.
Remote Events makes it possible to communicate between client (player) and the server

Put this in your localscript:
game.ReplicatedStorage.[your remote event here]:FireServer()

In a serverscript you can add this:
game.ReplicatedStorage.OnServerEvent:Connect(function()
game.SoundService.[your sound here]:Play()
end)

Let me know if this works okay?

I followed the steps and it seems that there are errors
localscript:
957ca8eae5239e6bab3c105343c325c4

severscript:
826139c1141f204ccf6375bd84410a8b

remove [ ] marks from the script.

Oh ok :sweat_smile: , but no sound is playing it does not work… :face_with_diagonal_mouth:

is there any error in the output ?

can you send me a photo of the sound’s properties ?

no error comes out, I can always push the player but no sound comes out

can you send a photo of properties of the sound ?

put a print line just aftee where you play the sound, to see if the code runs.

I added the line print but still nothing happens…

the print line was there to control if the script is working, what is the output now ?

yes I know , I mean it does not print , there is nothing at in the output

can you send me a photo of where is the script ?

try this code and send me the console

print("Script Works")

game.ReplicatedStorage.SoundEvent.OnServerEvent:Connect(function(plr)
    print("Fireee")
    game.SoundService.Sound:Play()
end)

it’s “TelekineticPush” ModuleScript
57acbb8dffd9a56d71cd90049d275caa

did you try my code ?

Why would you use sound service to emit the sound of a player’s head? Just use the local script that is firing to the server, and take its character. Insert a sound from a server script and let it play.

1 Like

yes, here is the output when I try with 2 players

can you send me the whole script in how you require it ?