How is this table getting integer/nil values?

I’ve been working on making a cash register for no reason at all, and I want it to be able to input the numbers into a gui. However, when defining the click detectors and putting them inside of a table, I’ve run into a problem. The 0, 1, 5, and 8 click detectors are put in the table just fine, but the rest of them are simply nonexistent. I have no idea what is happening with this, as after click detector 8, there is a value 0 and a value nil, and that’s it. Anyone know what to do about this?

The script:

local gui = script.Parent.Parent.AF1FCSWDDCDCRB_Price.AF1FCSWDDCDCRBP_Center.AF1FCSWDDCDCRBPC_Gui.AF1FCSWDDCDCRBPCG_TextLabel
local group = script.Parent:GetDescendants()
local clickDetectors = table.pack()
local partToKeep1
local partToKeep2
local partToKeep3

for _, v in pairs(group) do
	if v:IsA("ClickDetector") then table.insert(clickDetectors, tonumber(string.split(string.split(v.Name, "_")[1], "P")[2]) + 1, v) end
end

for i, v in pairs(clickDetectors) do
	v.MouseClick:Connect(function()
		partToKeep1 = string.sub(gui.Text, 3, 3)
		partToKeep2 = string.sub(gui.Text, 5, 5)
		partToKeep3 = string.sub(gui.Text, 6, 6)
		gui.Text = "$" .. partToKeep1 .. partToKeep2 .. "." .. partToKeep3 .. tostring(i - 1)
	end)
end

Now, important to note, if I set the clickDetectors value to {}, there is no errors, but the numbers are still there.

I’ll admit that I’m confused by that code :slight_smile: To me it looks like you made it a bit hard on yourself.
Hope you don’t mind if I forego debugging in favor of suggesting another way entirely:

local register = script.Parent
local buttons = register.Buttons:GetChildren() -- name the buttons 0 through 9 and give them click detectors, either in studio or in this code.
local sequence = {}

for _,button in next,buttons do
    button.ClickDetector.MouseClick:connect(function()
        local num = tonumber(button.Name)
        table.insert(sequence,num)
    end
end

Something like this would be enough to get the numbers isolated in a table.
When you want to add the decimal separator for cents, simply use #sequence-2 to find the point in the table at which it should be inserted. You could use table.insert() to put it directly in that spot, and then use table.concat() to bring the entire number together.

1 Like

I accidentally solved this myself, actually, although, your post did help, as it made me think to not try and order the clickDetectors properly, instead isolating their numbers when they are clicked. Thanks for that. Here is the code that I used:

local gui = script.Parent.Parent.AF1FCSWDDCDCRB_Price.AF1FCSWDDCDCRBP_Center.AF1FCSWDDCDCRBPC_Gui.AF1FCSWDDCDCRBPCG_TextLabel
local group = script.Parent:GetDescendants()
local clickDetectors = {}
local partToKeep1
local partToKeep2
local partToKeep3

for _, v in pairs(group) do
	if v:IsA("ClickDetector") then table.insert(clickDetectors, v) end
end

for _, v in pairs(clickDetectors) do
	v.MouseClick:Connect(function()
		partToKeep1 = string.sub(gui.Text, 3, 3)
		partToKeep2 = string.sub(gui.Text, 5, 5)
		partToKeep3 = string.sub(gui.Text, 6, 6)
		gui.Text = "$" .. partToKeep1 .. partToKeep2 .. "." .. partToKeep3 .. string.split(string.split(v.Name, "_")[1], "P")[2]
	end)
end

For context:

local gui = script.Parent.Parent.AF1FCSWDDCDCRB_Price.AF1FCSWDDCDCRBP_Center.AF1FCSWDDCDCRBPC_Gui.AF1FCSWDDCDCRBPCG_TextLabel

defines the gui to be changed

string.split(string.split(v.Name, "_")[1], "P")[2]

isolates the clickDetector’s number

partToKeep1 = string.sub(gui.Text, 3, 3)
partToKeep2 = string.sub(gui.Text, 5, 5)
partToKeep3 = string.sub(gui.Text, 6, 6)

gets the three numbers that need to be kept in the gui text

gui.Text = "$" .. partToKeep1 .. partToKeep2 .. "." .. partToKeep3 ..

moves the three numbers back a space, while keeping their value. Just like a real cash register/microwave

Hope this cleared up my code for you. Thanks for the help!

1 Like