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

Math Script Will Print 1-10 if numbers are 1-10 without being 2+ than last # wont work?

local Table = {1,2,3,4,"FDS",5,6,7,8,9,10}

local Result = nil

local FirstNums = {}
local LastNums = {}
local OtherNum = {}
for i,v in pairs(Table) do
	if tonumber(v) then
		if table.getn(LastNums) == 0 then
			table.insert(LastNums,v)
			table.insert(FirstNums,v)
		else
			if (FirstNums[table.getn(FirstNums)] + 1) == v then
				table.insert(LastNums,v)
			else
				table.insert(OtherNum,v)
			end
		end
	else
		print("NOT A NUMBER")
	end
end

for i = 1,table.getn(FirstNums) do
	if (FirstNums[i] + 1) == LastNums[i] then
		print(FirstNums[i].."-"..LastNums[i])
	else
		for _,v in pairs(OtherNum) do
			print(v)
		end
	end
end

doesn't print any errors

Output: NOT A NUMBER 3 4 5 6 7 8 9 10

2 comments

0

maybe your if statements are wrapped in () when they are unnecessary!

0

No, it worked before with the () around the FirstNums + 1, but i dont know why it isnt working anymore

1 answer

Not sure what your goal is but here's the script with my comments. As you can see, table.getn(FirstNums) is always 1.

local Table = {1,2,3,4,"FDS",5,6,7,8,9,10}

local Result = nil

local FirstNums = {}
local LastNums = {}
local OtherNum = {}
for i,v in pairs(Table) do
    if tonumber(v) then
        if table.getn(LastNums) == 0 then
			print("-Storing: ",v)
            table.insert(LastNums,v)
            table.insert(FirstNums,v)
        else
	        print("-FirstNum is always one: ",FirstNums[table.getn(FirstNums)])
			-- if 1 + 1 == v then
            if (FirstNums[table.getn(FirstNums)] + 1) == v then
				print("-Storing LastNum: ",v)
                table.insert(LastNums,v) -- Will always store 2
            else
				print("-Storing OtherNum: ",v)
                table.insert(OtherNum,v)
            end
        end
    else
        print("NOT A NUMBER")
    end
end

-- For loop iterates once
for i = 1,table.getn(FirstNums) do
	print("-First: ",FirstNums[i] + 1," : Last: ",LastNums[i])
	-- If 1 + 1 == 1 then
    if (FirstNums[i] + 1) == LastNums[i] then
        print(FirstNums[i].."-"..LastNums[i])
    else
		print("-OtherNum")
        for _,v in pairs(OtherNum) do
            print(v)
        end
    end
end

Did you mean to do this?

for i = 1,table.getn(LastNums) do
    if (FirstNums[1] + 1 == LastNums[i]) then
        print(FirstNums[1].."-"..LastNums[i])
    else
		print("-OtherNum")
        for _,v in pairs(OtherNum) do
            print(v)
        end
    end
end

2 comments

0

That still prints NOT A NUMBER 3 4 5 6 7 8 9 10 1-2

0

Let me know what it's supposed to print. It's hard to understand what you're saying.

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