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

How to make a string not affected by "-" or "%"?

Asked by 3 years ago
Edited 3 years ago

For more context I am trying to make a typing game and the way I am doing this is by using string.find to see if what the player typed is in the text they have to write the problem with using string.find or string.match or any other type of string functions is that if they include something like a "-" or a "%" before an invalid character in the pattern part it will return nil, so I want to know if there is a way to make a string not be affected by the dashes and what not.

local WordToWrite = "oil-based" -- Do not worry how I got oil-based as it is just an example
local TextBox = script.Parent 

TextBox.Changed:Connect(function()

    -- If the TextBox has something like "oil-b" or "oil-ba" it would return nil which is not what I want it to do

    print(string.find(TextBox.Text, WordToWrite))
    if string.find(TextBox.Text, WordToWrite) then
        script.Parent.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
    else
        script.Parent.BackgroundColor3 = Color3.fromRGB(255, 70, 70)
    end
end

I could just not include dashes, but I just want to know if there is a way around this before I delete any phrases including them.

1 answer

Log in to vote
1
Answered by
Speedmask 661 Moderation Voter
3 years ago
Edited 3 years ago

there are two options. the first is escaping characters. if you don't know what a pattern is, it's like a special string that can match more than just characters, but specific combinations of them, at the cost of special characters like those. roblox is basically using your original word as a pattern. if there is ever a special character, you can match the actual character by putting a backslash before it like \% rather than %, so in your original word, you could write "oil\-based"

otherwise, and probably simpler, the string documentation says you can parameterize the string.find method to ignore that stuff by writing true at the end, which checks in plaintext (throws the pattern stuff out the window). try replacing string.find(TextBox.Text, WordToWrite) with string.find(TextBox.Text, WordToWrite, 1, true).

0
Thank you so much!! Rayguyban2 89 — 3y
Ad

Answer this question