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.
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)
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.