#Script to make sound play from player after player says sound name in chat not working as intended.

1 messages · Page 1 of 1 (latest)

brittle pollen
#

`print("Voice Sounds Script")

local chat = game:GetService("TextChatService").OnIncomingMessage
local HelpSound = script:FindFirstChild("Help")
local PlayerName = game.Players.LocalPlayer.Name
local Head = game.Workspace.PlayerName.FindFirstChild("Head")

if chat == "Help" then
HelpSound.Parent = Head
wait(3)
HelpSound.playing = true

end

if HelpSound.Parent ~= Head then
print("HelpSoundDidNotMove")
wait()
end

if HelpSound.Playing == false then
print("NotPlaying")
wait()
end`

Script is child of starter player scripts not character. The sound is a child of the script itself. I'm very new to scripting I know the basics but this is my attempt at writing something myself without any tutorials so please don't clown on me 😅 . The sounds just doesnt play when I test nothing happens.

proud whale
#

You should have it in starter character also
U can use this instead

local plr = game.Players.LocalPlayer
local Character = script.Parent

plr.Chatted:Connect(function(message)
— here you play the message
end)

#

Also you will be the only one to hear it

cedar arch
#

on coming event isnt a string

proud whale
#

So I reccommend it in a serverscript

cedar arch
#

also rather than doing player name

#

do local player. charachter

brittle pollen
#

oh ok thankyou

proud whale
#

So

  1. Get when player joins (PlayerAdded)
  2. Check if they sent message (Player.Chatted)
  3. Check if message is "help"
  4. Check if they have a character and head
  5. Copy a sound, put it in head, play it. Wait 5 or so seconds and remove it
    If u have more questions let me know
cedar arch
brittle pollen
proud whale
brittle pollen
#

`local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Player = game:GetService("PlayerAdded")
local PlayerTalked = Player.PlayerChatted
local HelpSound = ReplicatedStorage:WaitForChild("Sounds"):WaitForChild("Help")
local Character = script.Parent
local HelpClone = HelpSound:Clone()

if PlayerTalked == "Help" then
HelpClone.Parent = Character:WaitForChild("Head")
HelpClone.Playing = true
if Character:WaitForChild("Head") == nil then
print("Charecter has no head")
end
wait(5)
HelpClone.Ended:Connect(function()
HelpClone:Destroy()
end)

end`

#

"10:42:10.718 Workspace.ohupe.VoiceSounds:3: attempt to index nil with 'PlayerChatted' "

#

ok i fixed error but code itself still doesnt work

winged raptorBOT
#

studio** You are now Level 1! **studio

brittle pollen
#

`local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Player = game:GetService("PlayerAdded")
local PlayerTalked = game.Players.PlayerChatted
local HelpSound = ReplicatedStorage:WaitForChild("Sounds"):WaitForChild("Help")
local Character = script.Parent
local HelpClone = HelpSound:Clone()

if PlayerTalked == "Help" then
HelpClone.Parent = Character:WaitForChild("Head")
HelpClone.Playing = true
if Character:WaitForChild("Head") == nil then
print("Charecter has no head")
end
wait(5)
HelpClone.Ended:Connect(function()
HelpClone:Destroy()
end)
`
end

proud whale
#

Ok so

#

Player.PlayerAdded is an event

#

Which means

#

It will make code happen under certain circumstances

#

Instead of local player = …

proud whale
winged raptorBOT
#

studio** You are now Level 5! **studio

proud whale
#

How to use the event:

function playerAdded(player)

print("Hey there someone joined. Name: "..player.Name

end

game.Players.PlayerAdded:Connect(playerAdded)

OR…

game.Players.PlayerAdded:Connect(function(player)

print("Hey there someone joined. Name: "..player.Name

end)

#

Player.Chatted is also an event

So in the event with the player added, add

player.Chatted:Connect(function(message)
print(message
end)

#

How it would look like ultimately

game.Players.PlayerAdded:Connect(function(player)
player.Chatted:Connect(function(message)
print(message)

—here you can have your code
end
end)

Another thing I noticed is that you checked for the head after the sound while it should be before making the sound. If there no head or character, no sound.
Also use FindFirstChild when checking for if a part is not nil

— this should be inside the player.Chatted event

local Char = player.Character
if Char and Char:FindFirstChild("Head") then
— do the thing now with the sound
end

#

Also
Instead of waiting 5, then checking for the ending of the sound, skip waiting 5 and just remove the sound after ending

And that event (sound.Ended) will only be fired once
And u only want to wait dor it’s end

So do
YourSoundHere.Ended:Wait()

brittle pollen
#

I got it working with the help of chatgpt thankyou for your help it basically said the same thing you said