Play audio from an emote for everybody

Hello so I’m making a small thing and I’m trying to have it so when I press G i have an emote that plays audio. Currenty only the local player hears it (which is obvious since I have it in a local script) but I don’t really have a clue on how I do this.

I hope you can help me and already a big thanks in advance.

The current script (local)

local keyPressed = Enum.KeyCode.G --Another example Enum.KeyCode.N
local animationId = "7071652191" --Change to your animation Id
local sound = game.Workspace.Sounds.Laugh


local userInputService = game:GetService("UserInputService")
local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local h = character.Humanoid
local humanoid = character:WaitForChild("Humanoid")
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://"..animationId
local animationTrack = humanoid:LoadAnimation(animation)

userInputService.InputBegan:Connect(function(input,isTyping)
    if isTyping then return end
    if input.KeyCode == keyPressed then
        if not animationTrack.IsPlaying then
            animationTrack:Play()
            sound.TimePosition = 0.55
            sound:Play()
            h.WalkSpeed = 0
            humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, false)
            wait(5.2)
            h.WalkSpeed = 16
            humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, true)
        end
    end
end)
3 Likes

You can probably make a remote event, make it function by a script like-game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function()
workspace.Sounds.Laugh:Play()
end)
and then in your local script you can fire the even like this-
game.ReplicatedStorage.RemoteEvent:FireServer()

1 Like

You’re on the right track here, however in order for everyone to hear the sound, you’re gonna want your sound in a ServerScript, meaning you’re going to have to fire a Remote Event every time the “G” key is pressed:

local animationId = "7071652191" --Change to your animation Id

local userInputService = game:GetService("UserInputService")
local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local h = character.Humanoid
local humanoid = character:WaitForChild("Humanoid")
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://"..animationId
local animationTrack = humanoid:LoadAnimation(animation)

local remote = game.ReplicatedStorage:WaitForChild("RemoteEvent") -- Here's where your remote event variable is

userInputService.InputBegan:Connect(function(input,isTyping)
	if isTyping then return end
	if input.KeyCode == keyPressed then
		if not animationTrack.IsPlaying then
			animationTrack:Play()
			remote:FireServer() -- Fire the remote event to the server every time key is pressed
			h.WalkSpeed = 0
			humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, false)
			wait(5.2)
			h.WalkSpeed = 16
			humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, true)
		end
	end
end)

Then in your server script, you’re gonna wanna detect when that remote event is fired:

local soundToPlay = workspace.Sound -- Or wherever your sound is located

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player) -- Receive remote event call
	soundToPlay.TimePosition = 0.55 -- Not sure if you still want this in here, but you can remove if you want
	soundToPlay:Play()
end)

Let me know if this works or not. I tested this on an empty baseplate, and it seemed to work for me. I put my LocalScript in StarterCharacterScripts, so I’m not sure if that’ll make a difference or not.

3 Likes

Thanks, sorry for late reply. This seems to have worked.

1 Like