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

Variable i not setting to y+1?

I've been working on a typewriter effect which also deals with XML tags, the XML tag typewrtier effect overwrites the normal one to type out the tagged text. For some reason when I try to set i to y+1 (end of the XML tags so it does not just type them out again but normally) it does not work and instead just resumes from the point the normal typewriter was stalled at? Help?

local Texter = script.Parent
local Transp = script.Parent.Parent.Transparent
local Text
local FollowUp = 1
--[]{FINDER VARIABLES}[]-------------------
local DotFinder
local CommaFinder
local QuestionFinder
local ExclaimFinder


local ON = false
local m
local n
local s
local x
local y
local s2
local f
local before
local L
-------------------
function setText(word)
	Text = word
	
	
	for i = 1, #Text do				
		if ON == false then
		string.sub(Text, 1, FollowUp)
		Transp.Text = string.sub(Text, 1, i)
		wait()
			Texter.Text = string.sub(Text, 1, i)
		end
		
		
		if y ~= nil then --///[[This also does not work?? wtf--]]\\\
			i = y+1
			y = nil
		end

		
		m = string.find(Texter.Text, "<", i)
		if m ~= nil then
			ON = true
			n = string.find(word, ">", string.len(Texter.Text))
			s = string.sub(Text,m,n)
			x = string.find(word, "<", n+1)
			y = string.find(word, ">", x+1)
			s2 = string.sub(word,x,y)
			f = string.sub(word,n+1,x-1)
			before = string.sub(Texter.Text,0,string.len(Texter.Text)-1)
			for c = 1, #f do
				Transp.Text = before..s..string.sub(f, 1, c)..s2
				wait()
				Texter.Text = before..s..string.sub(f, 1, c)..s2
			end
			
			i = y+1 --///[[The i variable of the loop would never cahnge to y+1 and continue the typrewriter effect from the right spot for some reason???--]]\\
			
			m = nil
			ON = false
		end


	end
end

setText('Hey, can you find<font color="rgb(255,165,0)"> orange</font>?')

I've left out some parts, as they are not related. This is why there may be random variables which dont seem to do anything

7 comments

0

Even more weirdly, after doing some testing it seems i IS setting to y+1 but for some reason reverts to 18 (start of tagged area)???

0

The variable "i" is auto-set to conform with the condition presented in the generic loop. You do not have control over this variable. The statement "y ~= nil" responds with false as the variable was never assigned a value.

0

i think i is already taken by the loop, so its a local variable (i think)

0

It's early in the morning for me, if there is a better approach to this, please take it over my current solution. I would personally design a function to extract and organize all of the XML attributes within the text into an array consisting of the text itself and it's designated colour. Then, iterate through said array and append each letter from this text piece individually—

0

—while applying its respective properties. If you haven't figured out how to do so by the time I awake, I'll return here to help you out!

0

I can tell you that you're taking an unnecessarily complex approach to this though, which may result in a harder time trying to implement features. I'd suggest you start over to have an easier time.

0

@Ziffxture leave an answer so I can upvote you, your comment helped me get my final answer to the problem

2 answers

✓ Accepted answer

####This turned out a lot more sophisticated than I anticipated:/ ####Honestly, this could be less complex too ;)

The code below applies the practice I mentioned above of deconstructing and reconstructing the XML tag. This will type-write the text within a said tag. If you wish to incorporate this method, you'd have to find a way to attach this to the previous text outside the tag.

local TestString = "This apple is <font color='rgb(255, 0, 0)'>red</font>!"

for TextTruncate = 1, #TestString do
	---------------
	if (TestString:sub(TextTruncate):match("^<%w+")) then
		---------------
		local Tag = TestString:sub(TextTruncate, TestString:find("</%s>", TextTruncate))
		---------------
		local TagType = Tag:match("<(%w+)")
		local RichText = Tag:match(">([%w%s]+)<")
		local TagCondition = TestString:sub(TextTruncate):match("<%w+ ([%w%s%p]+'>)")
		---------------
		local TagTemplate = "<"..TagType.." "..TagCondition.."%s</"..TagType..">"
		---------------
		for RichTextTruncate = 1, #RichText do
			---------------
			print(TagTemplate:format(RichText:sub(1, RichTextTruncate)))
		end
	end
end

--> <font color='rgb(255, 0, 0)'>r</font>
--> <font color='rgb(255, 0, 0)'>re</font>
--> <font color='rgb(255, 0, 0)'>red</font>

Solution found, I simply defined a variable called 'itr' I then set it to 1 and increased it by 1 every time the for loop does a loop (at the end)

(Ziffxture's answer when he posts it will be marked as the answer though as he helped me get this final answer (in comments) )

final code: (without the unrelated parts, again)

local Texter = script.Parent
local Transp = script.Parent.Parent.Transparent
local Text
local FollowUp = 1

local ON = false
local m
local n
local s
local x
local y
local s2
local f
local before
local itr = 1
-----------------
local function setText(word)
	Text = word
	for i = 1, #Text do		
		if ON == false then				
			string.sub(Text, 1, FollowUp)
		Transp.Text = string.sub(Text, 1, itr)
		wait()
			Texter.Text = string.sub(Text, 1, itr)
			

			m = string.find(Texter.Text, "<", itr)
			if m ~= nil then
				ON = true
				n = string.find(word, ">", string.len(Texter.Text))
				s = string.sub(Text,m,n)
				x = string.find(word, "<", n+1)
				y = string.find(word, ">", x+1)
				s2 = string.sub(word,x,y)
				f = string.sub(word,n+1,x-1)
				before = string.sub(Texter.Text,0,string.len(Texter.Text)-1)
				for c = 1, #f do
					Transp.Text = before..s..string.sub(f, 1, c)..s2
					wait()
					Texter.Text = before..s..string.sub(f, 1, c)..s2
				end
				m = nil
				ON = false
				itr = y+1
			end		
		end	
		
		itr = itr + 1
		
-------------------------------------
	end
end

wait(5)
setText('Hey, can you find<font color="rgb(255,165,0)"> orange</font> Exp Text Exp text')

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