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

How come the 'for' loop doesn't detect a certain string in the code I am current using?

I had been experimenting with the for loop, and sub method, when an idea occurred in my head, Is it possible for the for loop to detect a certain string within a string? And if so, will it replace the word?, and, sadly, the code did detect the string within the string, however, it only replaced the first letter of the certain string, and not the whole. I am currently confused on why that is, because, I thought I had coded it right, but, sadly, it did not end up like I wanted it to, here is the code I used in my experiment;

local msg = "I'm a Test! :D";
local chk = "";
local find = "Test";
local Replacement = "string";

for i = 1, msg:len() do
	if msg:lower():sub(i,i+find:len()-1) ~= find:lower() then
		chk = chk .. msg:sub(i,i)
	else
		chk = msg:sub(i,i+find:len()) .. Replacement
		print("FOUND MATCH")
	end
	wait(.1)
	print(chk)
end

print(chk)

1 answer

You can just use string.find()

local msg = "I'm a Test! :D"
local find = "Test"
local replacement = "string"
local start, fin = string.find(msg, "Test")
print(string.sub(msg,1,start -1)..replacement..string.sub(msg,fin + 1))

Can't test this right now, so it might not work and I'll edit later.

2 comments

0

I tried using the 'find' method as well, but, sadly, that did not work either.

0

It should be pretty simple, I'll edit my answer later to show you how it's done

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