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

String not printing as intended. Help please?

I'm making a game that plays a camera animation when the player joins. I got tired of manually printing the CFrame's and formatting my table so I wrote a little tool to print the table with all the positions pre-formatted in table style with the click of a button.

This code should simply print -

local CameraPositions = {
	CFrame,
	CFrame,
	CFrame 
}

Instead it prints -

local CameraPosition = {

}

I've never really done string manipulation like this so the problem may be obvious but I'm not seeing it.

local CFrames = {}

local CoreGui = game:GetService("CoreGui")

local ScreenGui = Instance.new("ScreenGui", CoreGui)
local Frame = Instance.new("Frame", ScreenGui)
Frame.Size = UDim2.new(0.2, 0, 0.3, 0)
Frame.Position = UDim2.new(0.79, 0, 0.2, 0)
Frame.Style = Enum.FrameStyle.RobloxRound

local Button = Instance.new("TextButton", Frame)
Button.Size = UDim2.new(1, 0, 0.2, 0)
Button.Style = Enum.ButtonStyle.RobloxRoundDefaultButton
Button.Position = UDim2.new(0, 0, 0.79, 0)
Button.Text = "Add Position"

local Finish = Instance.new("TextButton", Frame)
Finish.Size = UDim2.new(1, 0, 0.2, 0)
Finish.Style = Enum.ButtonStyle.RobloxRoundDefaultButton
Finish.Position = UDim2.new(0, 0, 0.59, 0)
Finish.Text = "Done"

local Close = Instance.new("TextButton", Frame)
Close.Size = UDim2.new(0.2, 0, 0.2, 0)
Close.Style = Enum.ButtonStyle.RobloxRoundDefaultButton
Close.TextScaled = true
Close.Text = "X"

Close.MouseButton1Down:connect(function()
	ScreenGui:Destroy()
end)

Button.MouseButton1Down:connect(function()
	table.insert(CFrames, workspace.CurrentCamera.CFrame)
	print(unpack(CFrames))
end)

function Parse() -- Named parse because I though I would need to create a simple parser to format the string.
	local str = ""
	for i = 1, #CFrames do
		local str = str..tostring(CFrames[i])..",\n"
	end
	return str
end

Finish.MouseButton1Down:connect(function()
	local Table = Parse()
	print("local CameraPositions = {\n"..Table.."\n}")
end)

Thanks, AZDev

~ AZDev

1 comment

0

I should add, I did make sure the table was populated with CFrames as seen in the Button function. The unpacked table prints fine. The formatted table did not.

1 answer

✓ Accepted answer

This line is your problem:

-- line 41:
local str = str..tostring(CFrames[i])..",\n"

Notice you defined it as local. This creates a new variable, also called str, that just immediately goes out of scope and is lost.

It should simply be

str = str..tostring(CFrames[i])..",\n"

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