Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

When I press B, only I hear the sound, no one else. How do I make it work in an fe game?

if key.KeyCode == Enum.KeyCode.B then
	print ("Playing "..charactervalue.." sound")
			waiting = true
			if character:FindFirstChild("Head"):FindFirstChild("Boo Sound") == nil then
				local GhostNoobSound = Instance.new("Sound",character.Head)
				GhostNoobSound.SoundId = "rbxassetid://533427376"
				GhostNoobSound.Name = "Boo Sound"
				GhostNoobSound.Volume = 0.8
				GhostNoobSound.MaxDistance = 100
				GhostNoobSound:Play()
			else
				character.Head["Boo Sound"]:Play()
			end
			wait(1)
			waiting = false
end

It plays the sound for me, but other people can't hear it. I tried making an event on a server script, but I don't know how to get the player that pressed B. This script is a local script. Also, this isn't the full script, it's just the sound making part, so charactervalue and other stuff do exist.

4 comments

0

Use RemoteEvents and use FireServer() to send it to the Server. The OnServerEvent event will automatically have the client/player that sent the call as a parameter and any other parameters you provided with FireServer(). Once you play the sound on the server, it should play for all players.

0

But how do I make the sound play from the server script? How do I access the correct player's head's sound?

0

I put the server script into startecharacter, then found the player by saying script.Parent. But it still only lets me hear the sound. Why?

0

Ok, I have given up. I made it work, but it only worked for the first time. So when I pressed B, other people could hear it, but when I pressed it again, only I could hear it, even if I respawned. This makes absolutely no sense what-so-ever.

1 answer

✓ Accepted answer

Create a RemoteEvent, and place it inside ReplicatedStorage (both client and server has access to replicatedstorage)

Rename the RemoteEvent, lets call it CreateSound.

Local Script:

local CreateSound = game.ReplicatedStorage:WaitForChild('CreateSound') -- get the remote
local Character = game.Players.LocalPlayer.Character

--Create the sound and edit it's properties
function BooSound()
	--Fire the remote and send the Sound to the server

	if not Character.Head:FindFirstChild('Boo Sound') then -- create boo sound if nil
		CreateSound:FireServer('Boo Sound', 'rbxassetid://533427376', 0.8, 100) -- send the name of the sound, soundid, volume and maxdistance

	elseif Character.Head:FindFirstChild('Boo Sound') then 
		Character.Head:FindFirstChild('Boo Sound'):Play() -- plays 'Boo Sound' if not nil
	end
end

Server Script:

local CreateSound = game.ReplicatedStorage:WaitForChild('CreateSound') -- get the remote

CreateSound.OnServerEvent:connect(function(player, name, id, volume, maxdistance) -- the first parameter is always the player who fired the remote to the server

local Sound = Instance.new('Sound', player.Character.Head)
Sound.Volume = volume
Sound.MaxDistance = maxdistance
Sound.Name = name
Sound.SoundId = id

Sound:Play()
print(player.Name .. " played " .. Sound.Name)
end)

6 comments

0

I suggest you put the ServerScript in ServerScriptService and name it 'serverReciever' or whatever you want

0

Where are you calling the BooSound function?

0

It says attempt to index local 'sound' (a nil value). It can't find 'sound' in the server script on line 4.

0

It says that when I play, however it works in studio.

0

Edited it. Just switched the scripts so that the server script creates the sound

0

Thanks. Works now.

This discussion is preserved as it was. New answers, comments, and votes are closed.