Function won't go in order with dictionary

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    What I want to achieve is to make sure that the next() function actually goes in order (which I could try ipairs but not sure how I would put it into the script.)
    The order should move like this; 1,2,3,4,5, etc instead of it moving as a random selection or moving backward.

  2. What is the issue? Include screenshots / videos if possible!
    The issue is that when the function is fired, Instead of it going in order each time, It goes from the first option to the last and then goes in reverse, Which is quite strange. An image of the output shows that it goes from the 1st option To the very last as you can see here;

image

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I’ve tried using ipairs, but for some reason, but since this is a dictionary, I’m quite confused - They arent exactly the same as arrays but similar. I’ve also tried to look for solutions on developer hub, Which I copied the same exact script - well not exactly the same but in a way where it should work the same way and it didn’t work.

The script is provided below;

local camerasConfig = {
    ["PreviewCam"] = nil
    ,["TourCam1"] = "text input 1"
    ,["TourCam2"] = "text input 2"
    ,["TourCam3"] = "text input 3"
    ,["TourCam4"] = "text input 4"
}

local currentCam = config.CameraName -- config is a module script - CameraName is 'PreviewCam'
local nextCam, val = next(camerasConfig, currentCam)

local btn = script.Parent.button -- this button was for testing purposes to fire the function.

function moveToNext()
  print(currentCam, nextCam)
  currentCam = nextCam
    
  nextCam, val = next(camerasConfig, currentCam)
end

btn.MouseButton1Click:Connect(function()
  moveToNext()
end)

What’s supposed to happen is that it’s meant to go from PreviewCam to TourCam1 and so on in order. Although it’s not doing that and I don’t know why.

If you guys could help, I would be thankful.

Dictionaries do not have an order. Consider using an array instead or a proxy array that sorts the keys of your dictionary into said array. The former option is better than the latter though.

Arrays have numerical indices, so you can just work with an incrementing or decrementing number and access the array based on that number. Not sure if next respects an array’s order though, I tend to never call it directly.

Each time one of them is selected, a text input should be selected - which is why I made a dictionary.

How would I do this with a basic array. Would I just use normal if statements?

You could try putting the camera name and text into a table with number value keys and then just read the camera name and text from the table whenever you need to

local config = {
	CameraName = 1 --"PreviewCam"
}

local camerasConfig = {
	{name = "PreviewCam", text = nil},
	{name = "TourCam1", text = "text input 1"},
	{name = "TourCam2", text = "text input 2"},
	{name = "TourCam3", text = "text input 3"},
	{name = "TourCam4", text = "text input 4"}
}

local currentCam = config.CameraName
local nextCam, tab = next(camerasConfig, currentCam)

function moveToNext()
	print(tab.name, tab.text)
	currentCam = nextCam
	
	nextCam, tab = next(camerasConfig, nextCam)
	
	if nextCam == nil then
		nextCam, tab = next(camerasConfig, nextCam)
	end
end

for i = 1, 10 do
	moveToNext()
	wait(0.1)
end

image

1 Like

Thank you so much! It worked.

I’m very thankful for this solution, Saved me a lot of time.