I have this script that uses Animation Events to play a footstep sound everytime that it passes over it. It works completely fine, although it only works for the Client, and I would like it to be global instead so that all other players can hear them.
This is the code.
local RunAnimation = Instance.new('Animation')
RunAnimation.AnimationId = "rbxassetid://" -- Animation Id
RAnimation = Humanoid:LoadAnimation(RunAnimation)
RAnimation:GetMarkerReachedSignal("Step"):Connect(function()
script.Sound:Play()
end)
You should fire a RemoteEvent from that client that is playing the animation.
The RemoteEvent will be listened by the server, and play the sound in serverSide, probably the sound should be inside a part of the character that fired the event in order the sound is 3d and coming from the character and not general in workspace.
In that way all players in server will be able to listen the sound.
-- LocalScript
-- Fetch service & event
local RS : ReplicatedStorage = game:GetService("ReplicatedStorage")
local event : RemoteEvent = RS:FindFirstChild("SoundEvent")
-- Play sound when event fired: server -> client
event.OnClientEvent:Connect(function(sound)
sound:Play()
end)
-- Fire event client -> server
-- Pass sound as argument
RAnimation:GetMarkerReachedSignal("Step"):Connect(function()
event:FireServer(script.Sound)
end)
-- ServerScript
local RS : ReplicatedStorage = game:GetService("ReplicatedStorage")
local event : RemoteEvent = RS:FindFirstChild("SoundEvent")
-- Fire event server -> clients when event fired client -> server
-- Pass sound as argument
event.OnServerEvent:Connect(function(plyr : Player, sound : Sound)
event:FireAllClients(sound)
end)
Thats exactly what I said, but, if the sound is a child of the script, that sounds will not be 3d and not coming from the character that triggered the event, that will cause having a general sound on all clients, not even knowing where that sound is comming from
Alright, I did all of the steps that you said, but the sound still plays in 2D, how would I go about making the sound 3D (Playing the sound inside the character, that is)
2D Are sounds that are not Parented to an Attachment, BasePart, (anything 3D basically)
Parent the Audio to an Instance and that should give you a 3D sound
I am using a server script, I just need to know how to get the sound INTO the player, and then play it there
Script Im Using:
local event = game.ReplicatedStorage.FootstepEvent
local Sound = script.Sound
event.OnServerEvent:Connect(function(Plr)
local Player = Plr
local CHR = Player.Character
print(Player.Name)
Sound.Parent = CHR
wait()
Sound:Play()
end)