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

How would you make a sound/block be placed/played if a certain word is said in chat?

I've been working on a game, and I need a little help. Personally, I know a little bit of coding myself just not in Lua. I'm trying to make it to where,

1. When a player in game says a keyword in chat, (E.g: Cow)

It will play a sound, and place a block in the world/change the transparency of it.

Anybody that could help me with my problem?

1 answer

you can use the Chatted event and then check if the message parameter of it is a keyword. and make a dictionary of the keywords with a nested array for each keyword value and in it would be carrying the transparency of the part and the soundID it should play.

--server

--create a dictionary for the keywords
local keywords = { ["cow"] = {soundId = 00000, transparency = 0.2} }

game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(realMsg)
		local msg = string.lower(realMsg) -- in case it has capital letters

		for keyword,info in pairs(keywords) do -- iterate through the dictionary
			if keyword == msg then
				spawn(function()
					local sound = Instance.new("Sound")
	
					sound.SoundId = "rbxassetid://"..info.soundId
					sound.Parent = workspace
					sound:Play()
					sound.Stopped:Wait() -- wait until the sound stopped playing
					sound:Destroy()
				end)
				local part = Instance.new("Part")

				part.Transparency = info.transparency
				part.Anchored = true
				part.Parent = workspace
				
				break -- breaks the loop
			end
		end
	end)
end)

also the keyword dictionary index has to be all lowercase since string.lower() turns all the letters inside a string to lowercase.

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