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

How to find a space in a string as in " "?

I'm working on a custom made Chat Gui and I would like it so that the chat doesn't cut through a word when I send it to the next line(, so that it doesn't cover the entire screen like when somebody has something long to say.) This is what I have so far.

a=Instance.new("ScreenGui",game.StarterGui)
a.Name="ChatGui"
Gui={"a1","a2","a3","a4","a5","a6","a7","a8","a9","a10"}
for i=1,#Gui do
	local d=Instance.new("TextLabel",a)
	d.Size=UDim2.new(0,500,0,25)
	d.Position=UDim2.new(0,10,0,(i*20)+150)
	d.Name=Gui[i]
end
for i,v in pairs(a:GetChildren()) do
	v.Text=""
	v.BackgroundTransparency=1
	v.TextXAlignment="Left"
	v.FontSize="Size14"
end
function Chat(plr,chat)
	if game.StarterGui.ChatGui~=nil then
		b=game.StarterGui.ChatGui
		if #chat<40 then
			b.a1.Text=b.a2.Text
			b.a2.Text=b.a3.Text
			b.a3.Text=b.a4.Text
			b.a4.Text=b.a5.Text
			b.a5.Text=b.a6.Text
			b.a6.Text=b.a7.Text
			b.a7.Text=b.a8.Text
			b.a8.Text=b.a9.Text
			b.a9.Text=b.a10.Text
			b.a10.Text=plr..": "..chat
		elseif #chat>40 then
			b.a1.Text=b.a3.Text
			b.a2.Text=b.a4.Text
			b.a3.Text=b.a5.Text
			b.a4.Text=b.a6.Text
			b.a5.Text=b.a7.Text
			b.a6.Text=b.a8.Text
			b.a7.Text=b.a9.Text
			b.a8.Text=b.a10.Text
			b.a9.Text=plr..": "..string.sub(chat,1,40)
			b.a10.Text="(Continued): "..string.sub(chat,41,80)
		end
		for i,v in pairs(game.Players:GetPlayers()) do
			d=b:clone()
			v.PlayerGui.ChatGui:Destroy()
			d.Parent=v.PlayerGui
		end
	end
end
for i,v in pairs(game.Players:GetPlayers()) do
	if v.PlayerGui:FindFirstChild("ChatGui")==nil then
		c=a:clone()
		c.Parent=v.PlayerGui
		v.Chatted:connect(function(msg)
			Chat(v.Name,msg)
		end)
	end
end
game.Players.PlayerAdded:connect(function(nP)
	nP.Chatted:connect(function(msg)
		Chat(nP.Name, msg)
	end)
end)

1 answer

✓ Accepted answer

I believe this should be of assistance: String Patterns - Character Classes

s = 'chat spacing chat spacing chat spacing chat spacing chatspacing chatspacing' 
	-- example chatted dialog
n = 20 -- characters

p = ''
for i = 1, n do 
p = p..'.' 
end 

p = p..'%s' 
t = s:match(p)
textbar_1 = string.sub(s,1,(#s)-(#t))
textbar_2 = t

-- you can iterate between checkpoints if you have to,
-- but I'm sure you'd limit it appropriately otherwise.

print(textbar_1,textbar_2)

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