Chat function ignores anything after space

I’m trying to make a chat activated season change system, but no matter what I’ve tried I can’t get it stop ignoring everything after the first word, I tried stuff like :lower() and a few other things but didn’t work. I really haven’t messed with chat systems much so I’m probably doing something dumb. It’s ignoring anything after the first word so it always activates the first if statement. For example if I type “Change 2” it will activate the function for “Change 1” because its before it.

Heres the part of the code that troubling me:

Player.Chatted:Connect(function(text)
		if text == "change 1" or "Change 1" then 
			season1Change()
			Animation:Play()
		elseif text == "change 2" or "Change 2" then
			season2Change()
			Animation:Play()
		elseif text == "change 3" or "Change 3" then
			season3Change()
			Animation:Play()
		elseif text == "change 4" or "Change 4" then
			season4Change()
			Animation:Play()
		end
		end)

The issue is that the second part of your condition, or "Change 1", will always be true every time. To fix that, you need to compare the string “Change (numberHere)” to the value of the text variable like so:

if text == "change (numberHere)" or text == "Change (numberHere)" then

If you want to make your code easier to read, then you should set the text variable to be equal to the returned value when calling string.lower.

Player.Chatted:Connect(function(text)

   text = string.lower(text)
		
   if text == "change 1" then 
	season1Change()
	Animation:Play()
   elseif text == "change 2" then
	season2Change()
	Animation:Play()
    elseif text == "change 3" then
	season3Change()
	Animation:Play()
    elseif text == "change 4" then
	season4Change()
	Animation:Play()
    end
	
end)
2 Likes

A little off topic but,

With what @UnderMyWheel said, you could also minimize the code by removing all of those if statements and instead match for “change” in the front, and a digit in the back, and then call the function associated with that given digit.

A less hardcoded method would also allow the freedom to easily edit the code.

Could be something like this:

local seasonFunctions = {
    ["1"] = season1Change,
    ["2"] = season2Change,
    ["3"] = season3Change,
    ["4"] = season4Change
}

Player.Chatted:Connect(function(text)
    text = text:lower()

    if text:match("^change") then
        local season = text:match("%d+$")
        local seasonFunction = seasonFunctions[season]

        if seasonFunction then
            seasonFunction()
            Animation:Play()
        end
    end
end)

If you’re wondering how the match("^change") and match("%d+$") works, it works by first checking if change is at the start of text; ^ means check the front. %d+ means to get every digit in sequence essentially, and $ is the opposite of ^; check the end.

Here is a resource on string patterns: Strings | Documentation - Roblox Creator Hub

4 Likes

A better way to check for this while capturing the number would be using captures, like so:

local season = text:match("^change (%d+)$")
if season then -- and so on
end

This way you only have to use one pattern and one :match call rather than separating it into two parts. This change also means that it won’t work for stuff like “change something 1”, which your example would work for.

5 Likes