I’m trying to make it so that when a user sends a message, it emits a sound via the user’s head. The first part of the code works completely fine, though it’s broken when it comes to plr.Chatted. I don’t know how to fix it since I’m unfamiliar with it.Chatted.
Does any help please?
local plr = game.Players.LocalPlayer
plr.CharacterAdded:Connect(function(char)
print("Character Loaded.")
local head = char:WaitForChild("Head")
local clone = replicated.chatsound:Clone()
clone.Parent = head
end)
plr.Chatted:Connect(function(msg)
print(plr.."sent a message.")
plr.Character.Head.chatsound:Play()
end)
This is the full script. It’s a LocalScript on StarterPlayerScripts.
local plr = game:GetService("Players").LocalPlayer
plr.CharacterAdded:Connect(function(char)
print("Character Loaded.")
local head = char:WaitForChild("Head")
local clone = replicated.chatsound:Clone()
clone.Parent = head
end)
plr.Chatted:Connect(function(msg)
print(plr.Name.."sent a message.")
local Character = plr.Character or plr.CharacterAdded:Wait()
Character:WaitForChild("Head").chatsound:Play()
end)
Try changing it to a server script and do this instead.
game.Players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function(char)
print("Character Loaded.")
local head = char:WaitForChild("Head")
local clone = replicated.chatsound:Clone()
clone.Parent = head
end)
plr.Chatted:Connect(function(msg)
print(plr.."sent a message.")
plr.Character.Head.chatsound:Play()
end)
end)
None of the codes worked for some reason. I even tried to switch to LegacyChat, tried changing to a normal script, moving it to explorer… to no avail.
Also @Pseudoisochromatic , what do you mean exactly if you don’t mind? As I said before, I’m still very unfamiliar with .Chatted so it might take me a while to understand what I have to do. I tried switching it to a normal script and changing its RunContext to Server but still no use…
@052008cooljp is right, I just fixed some things. Put this in a regular script in ServerScriptService.
game:GetService(“Players”).PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function(char)
print("Character Loaded.")
local head = char:WaitForChild("Head")
local clone = replicated.chatsound:Clone()
clone.Parent = head
end)
plr.Chatted:Connect(function(msg)
print(plr.Name.." sent a message.")
plr.Character.Head.chatsound:Play()
end)
end)
She was saying that the .Chatted event won’t work on the client so it won’t work for LocalScripts which are client based, it will work on the server which are just normal scripts that just have the class name Script. Which is why it has to be a server script in code @AlbertSeir mentioned above.