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

What is a script for changing the color of a brick when typing a chat command?

How can I change the color of a brick when typing any command on roblox.

For example, if I said "hello" in chat, a white brick would turn into a black brick.

Thank you.

2 comments

0

This is not a request site

0

^

1 answer

##Question

What is a script for changing the color of a brick when typing a chat command? For example, if I said "hello" in chat, a white brick would turn into a black brick.

##Answer A player instance has .Chatted signal which you can connect a function to. The connected function has the message the player chatted passed through.

(This will be a lengthy post) (If you don't feel like reading the entire post I put an uncopylocked place at the end you can look at, just accept this answer if it helped you!) (By the way your question might get closed since it isn't really constructive; ill help you in the meantime thought!)

###Example Code To Print What A Player Chats

local players = game:GetService("Players")

local function printChat(message)
	print(message)
end

local function playerAdded(player)
	player.Chatted:Connect(printChat)
end

players.PlayerAdded:Connect(playerAdded)

###Solution To Your Question We can check our argument message which is a string and see if the message is equal to hello.

Now how do we do this? We can compare strings with if statements like any other datatype.

####Example

local a = "this is a string"
local b = "this is also a string"
if a == b  then
	print("yes")
else
	print("no")
end

This example code would result in "no" being printed since a ("this is a string") isn't equal to b ("this is also a string"). And this makes sense since the two strings are different. They don't have the exact same phrase and words. We can apply this statement checking to a function that checks if the player's message is "hello". We can also make the entire message lowercase so players can say "HELLO" or "HeLLo" and it still works!

local players = game:GetService("Players")

local function checkForCommand(message)
	local command = message:lower()

	if command == "hello" then
		print("player called command")
	end
end

local function playerAdded(player)
	player.Chatted:Connect(checkForCommand)
end

players.PlayerAdded:Connect(playerAdded)

Now we have something so everytime the player says "hello" in any casing it will print "player called command" into the output. Next we can set a variable to the part we are going to change the color of then we can check if its white or black then toggle it depending on what color it is! We are going to set a variable to keep track of the color it is currently so we can toggle it. Since the brick starts off white we can set a variable isWhite to true.

local players = game:GetService("Players")

local part = workspace:WaitForChild("ColorPart")
local isWhite = true --// our toggling variable 

local function togglePartColor()
	if isWhite then --// check if white
		isWhite = false --// set to false since we are making black
		part.Color = Color3.new(0, 0, 0) --// set part color to black
	elseif not isWhite then --// check if black
		isWhite = true --// set to true since we are making white
		part.Color = Color3.new(1, 1, 1) --// set part color to white
	end
end

local function checkForCommand(message)
	local command = message:lower() --// make message lowercase

	if command == "hello" then --// check if its our command
		togglePartColor() --// if its the command then toggle part's color
	end
end

local function playerAdded(player)
	player.Chatted:Connect(checkForCommand) --// connect chat event
end

players.PlayerAdded:Connect(playerAdded) --// connect player added so we can get reference to players who join

##Extra

Uncopylocked place with code you can edit and mess around with. https://www.roblox.com/games/2657205382/chat-to-change-white-brick-to-black

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