Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How can I get my script to play the sound for all messages including the word help?

Asked by 6 years ago

In my game, I wanted is so that when players say something in a message a sound would play. I have a script that will play a sound when they say something, but only if it is that specific something and nothing else. if players say "help" then the sound will play, but if they say "can someone help me?" nothing will happen... How would I get the script to play the sound for all messages including the word help? Here is the script:

function onPlayerEntered(newPlayer)
    newPlayer.Chatted:connect(tutorialChatted)
end

game.Players.PlayerAdded:connect(onPlayerEntered)

function tutorialChatted(msg)
    if (msg=="help") then
        local mm = workspace:findFirstChild("Help Sound")
            mm.Playing = true

    end
end
0
Just remove line 8 and 12 User#2146 0 — 6y
0
but I only want the sound to activate when players say something with help in it, just not restricted to only saying help PikminLegion 38 — 6y

1 answer

Log in to vote
0
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
6 years ago
Edited 6 years ago

You can use the string.match function, or the string.find function to determine if your string contains the specified substring.

note: define constant values outside of your events! this way, it will only be defined once rather than everytime the event is called.
local mm = workspace:FindFirstChild("Help Sound") --Define outside event

function onPlayerEntered(newPlayer)
    newPlayer.Chatted:connect(tutorialChatted)
end

game.Players.PlayerAdded:connect(onPlayerEntered)

function tutorialChatted(msg)
    if msg:match("help") then --'match' function
        mm:Play()
    end
end
Ad

Answer this question