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

How would I add a sound to this that comes out of the players head so others can hear it?

local player = game.Players.LocalPlayer
repeat wait() until player.Character.Humanoid
local humanoid = player.Character.Humanoid
local mouse = player:GetMouse()


local anim = Instance.new("Animation")
anim.AnimationId = "http://www.roblox.com/asset/?id=4580831928"

mouse.KeyDown:connect(function(key)
  if key == "z" then
   local playAnim = humanoid:LoadAnimation(anim)
   playAnim:Play()

    end
end)

Here is the script so far, it's supposed to be a taunt and I need sound to come from the players head to make it sound realistic. Any help is greatly appreciated :))

2 answers

Based on the variable player, this script must be a localscript, Two choices I am currently thinking of:

  1. Move the whole code into a regular script and reconfigure so that the script is compatible server sided.
  2. Make another script, add some remotes and configure the regular script so it adds sounds and plays them. Here's a page explaining Remote Functions and Remote events: https://developer.roblox.com/en-us/articles/Remote-Functions-and-Events

Here's an example of usage of remotes.

Local script

local RE=game:GetService('ReplicatedStorage'):WaitForChild('RemoteEvent');
RE:FireServer('wow FE');

Server Script

local RE_1=Instance.new('RemoteEvent',game:GetService('ReplicatedStorage'));
RE_1.OnServerEvent:Connect(function(pl,a)
	print(tostring(a));--> 'wow FE'
end);

2 comments

0

lol idk why you need to use tostring for a string.

0

its supposed to represent that you can pass multiple values but i didnt want to discuss that

local player = game.Players.LocalPlayer
repeat wait() until player.Character.Humanoid
local humanoid = player.Character.Humanoid
local mouse = player:GetMouse()
local taunt = game.ReplicatedStorage.taunt

local anim = Instance.new("Animation")
anim.AnimationId = "http://www.roblox.com/asset/?id=4580831928"

mouse.KeyDown:connect(function(key)
  if key == "z" then
   local playAnim = humanoid:LoadAnimation(anim)
   playAnim:Play()
   taunt:FireServer(1)
    end
end)

----// server
local Remote = game.ReplicatedStorage.taunt
Remote.OnServerEvent:Connect(function(p, taunt)
for i,v in pairs (game.ReplicatedStorage.Sounds:GetChildren()) do
if v.Name == "taunt"..taunt then
local sound = v:Clone()
sound.Parent = p.Character.Head
sound:Play()
sound.Ended:Connect(function()
game:GetService("Debris"):AddItem(sound, 0.1)
end)
end
end
end)

3 comments

0

basic view of what u wanted

0

I got an error in output saying 20:55:18.884 - OnServerEvent can only be used on the server

0

lol u put the ---// server part on a serverscript, not a localscript. The type of script you're looking for is the plain script, called a serverscript

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