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

Way to find certain characters without a class? {SOLVED}

I am attempting to find more than one character in a string. I was thinking of using string.match() but I don't think that's right.

I need a function that will find the characters that I put in in a string.

I need to match a space, a comma, a period, a question mark, and an exclamation mark, but I don't want to use %p in my code because that gets all punctuations and I just want those ones.

In my code, if I type in a punctuation or a space and type normal characters, the output still says that I typed a punctuation or space.

Example text that I wrote into my script: "Hello, this is text!"

Output:
You pressed enter

wasn't punctuation or space so it played sound (x5)

was a punctuation or a space so it didn't play sound (x15)

Here's the code I'm attempting:

local sound = game.SoundService:WaitForChild("KeyboardSound")
local writeBox = script.Parent
local autoWriteBox = writeBox.Parent:WaitForChild("AutoWriteBox")

script.Parent.FocusLost:Connect(function(ep, iTCFL) --bool enterPressed, inputThatCausedFocusLoss
	if ep then
		print("You pressed enter")
		for i = 1, #writeBox.Text, 1 do
			autoWriteBox.Text = string.sub(writeBox.Text, 1, i)
			local match = string.match(string.sub(writeBox.Text, 1, i), "[%p%s]+") -- what can I change?
			
			if match == nil then
				print("wasn't punctuation or space so it played sound")
				game.SoundService:PlayLocalSound(sound)
				
			else
				
				print("was a punctuation or a space so it didn't play sound ")
			end
			wait(0.1)
		end
	end
end)

1 answer

I have solved this problem and completed my game!

Code:

--------------------- Define everything
local soundService = game:GetService("SoundService")
local sound = game.SoundService:WaitForChild("KeyboardSound")
local writeBox = script.Parent
local autoWriteBox = writeBox.Parent:WaitForChild("AutoWriteBox")
local startingString1 = "Hi, this is Green_Lime2 and you will be testing out writing like a type writer in ROBLOX!"
local startingString2 = "Just write something below and I'll write it for you!"
local startingString3 = "Have fun writing!"

--------------------- Write 1st string when a player joins the game

for i = 1, #startingString1, 1 do
	autoWriteBox.Text = startingString1:sub(1, i)
	wait(0.1)

	if startingString1:sub(i, i) == "." or startingString1:sub(i, i) == "," then
		wait(0.5)

	elseif startingString1:match("%w") then
		soundService:PlayLocalSound(sound)
	end
end

wait(1)

--------------------- Write 2nd string

for i = 1, #startingString2, 1 do
	autoWriteBox.Text = startingString2:sub(1, i)
	wait(0.1)

	if startingString2:sub(i, i) == "!" then
		wait(0.5)

	elseif startingString2:match("%w") then
		soundService:PlayLocalSound(sound)
	end
end

wait(1)

--------------------- Write 3rd string

for i = 1, #startingString3, 1 do
	autoWriteBox.Text = startingString3:sub(1, i)
	wait(0.1)

	if startingString3:sub(i, i) == "!" then
		wait(0.5)

	elseif startingString3:match("%w") then
		soundService:PlayLocalSound(sound)
	end
end
wait(1)

autoWriteBox.Text = ""
writeBox.Visible = true
--------------------- Write whatever a player writes in WriteBox

function typeWrite(s)

	for i = 1, #s, 1 do
		autoWriteBox.Text = s:sub(1, i)
		wait(0.1)

		if s:sub(i, i) == "!" or s:sub(i, i) == "." or s:sub(i, i) == "?" or s:sub(i, i) == "," then
			wait(0.5)

		elseif s:match("%w") then
			soundService:PlayLocalSound(sound)
		end
	end
end

--------------------- Runs when player presses enter and writes what player types in WriteBox then clears characters in WriteBox

script.Parent.FocusLost:Connect(function(ep, iTCFL) --bool enterPressed, inputThatCausedFocusLoss
	if ep then
		print("You pressed enter")
		typeWrite(writeBox.Text)
		writeBox.Text = ""
	end
end)  

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