Sound for multiple people

So im trying to make an emote thing but the dancing sound is only for one player. I know why because its an local script but if i use an normal script it doesnt work anymore. So how do i make the music play for everyone? (The sound is coming from the players torso)

Heres the code i am using

local btn = script.Parent
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local hum = char:WaitForChild(“Humanoid”,10)
local dance = hum:LoadAnimation(script:FindFirstChild(“Anim1”))
local music = char.Torso.Sound
local CanEmote = true

btn.MouseButton1Click:connect(function()
if CanEmote == true then
music.SoundId = “rbxassetid://1845736826”
music.Volume = 1
music.Playing = true
music.Looped = true
hum.JumpPower = 0
hum.WalkSpeed = 0
CanEmote = false
dance:Play()
else
music.Volume = 0
hum.JumpPower = 50
hum.WalkSpeed = 16
CanEmote = true
dance:Stop()
end
end)

You can use Remote Events and on the server side play the sound and people will hear it.
[Fire the remote using FireServer() method when the emote is activated. Call it on the server by using OnServerEvent].

Technically, you could still use a normal script for that, but when it’s coming to animations it’s mostly advised to play the animation on a local script since it’s also replicating to the server.

If you used a normal script,
and that script is under StarterCharacterScripts it means it’ll be inside the player’s character when they’re in-game, so you could simply access the character in this case like the following:

local char = script.Parent
local hum = char.Humanoid
--rest of the code
1 Like

You can’t get the player via game.Players.LocalPlayer in a server script, if you were trying to.

What I’d recommend doing is putting a normal script in StarterCharacterScripts and then to get the player’s torso and play a sound from it you just need to do this:

local char = script.Parent
local torso = char:WaitForChild(“UpperTorso”) --or just ”Torso” for R6.

-- Whatever you want to do to add the sound to the torso and play it here.

If you still need to get the player from this script, for whatever reason, you’d do this:
local plr = game.Players:GetPlayerFromCharacter(char)

1 Like

yeah or he could also do :GetPlayerFromCharacter() and keep the script in the character

1 Like

was just adding that lol, but if he played it from a localscript it would only play for the person the script is in

1 Like

This is a raw script pair you can play around with. There is a sound in the character named Sound.
These are two scripts firing a remote to play it.

-- LocalScript - (used locally like from a gui)
local remoteEvent = game.ReplicatedStorage:WaitForChild("PlaySoundRemote")
local function playSound() remoteEvent:FireServer() end
script.Parent.MouseButton1Click:Connect(playSound)

-- Script - (in ServerScriptService)
local remoteEvent = Instance.new("RemoteEvent")
remoteEvent.Name = "PlaySoundRemote"
remoteEvent.Parent = game.ReplicatedStorage
-- or you could just make the Remote and place it in ReplicatedStorage

local function playSound(player)
    local sound = player.Character:FindFirstChildWhichIsA("Sound")
    if sound then sound:Play() end
end
remoteEvent.OnServerEvent:Connect(playSound)

The LocalScript simply fires a remote located in ReplicatedStorage.
This then triggers the Script with OnServerEvent that then plays the sound.

1 Like