Why does my table function not work?

local Players = game:GetService("Players")
local Teams = game:GetService("Teams")
local TweenService = game:GetService("TweenService")

local LocalPlayer = Players.LocalPlayer
local PlayerGui = LocalPlayer.PlayerGui

local jobView = PlayerGui:FindFirstChild("JobView").BG_Frame

local cameraRegions = workspace.CameraView

local cameraTable = {
	policeOfficer = cameraRegions.PoliceOfficer,
	doctor = cameraRegions.Doctor,
	Cafe = cameraRegions.Cafe
}

local camera = workspace.CurrentCamera

camera.CameraType = Enum.CameraType.Scriptable



local function nextButtonClicked()
	for i, v in pairs(cameraTable) do
		v = next(cameraTable)
		print(v)
		end
end

jobView.NextClick.MouseButton1Down:Connect(nextButtonClicked)

I am trying to make a table, and I am testing it to see if it prints (V), which it does not. I do not have an error.

The value V automatically updates in the for loop, so using v=next(cameraTable) is unnecessary, try commenting out that line.

I am trying to make when the player clicks on the next button, it goes to the next value in the table, so it would still do that after removing it?

tested it: still nothing is printed, no error

Since cameraTable is not empty, printing the contents of it will print something. Since nothing prints, that means nextButtonClicked is never called. Did you click on the jobView gui? do you know whether this script runs at all?

ok, fixed it, I thought it would work in replicatedstorage (which it didn’t). An issue which I’m having is that now I see

  Doctor
  Cafe
  policeOfficer

In the console instead of just

doctor

or the next value in the table.

In your cameraTable table, you are explicitly assigning values to indices (or properties). The “i” (index) in the for loop is the property the iterator function is currently on (in this case pairs) and the “v” (value) is the value the iterator function is on. The reason it’s printing out “Doctor” is because you’re printing out each value of the table instead of each index (or property). My guess is it prints out “Doctor” because casting an instance to a string usually results in just the name of the instance, not the full path.

To access each index (which is what it sounds like you want to do) just replace v with i in your print function call.

Hope this helps!

I am trying to make it print the next item in the table each time I click on the next button. If I click the next button from 0, it will print 1. From 1, it prints 2 and so on

In this case, I would make a separate variable that gets incremented like so every time you click the button:

local currentSubject = 1
...
--- Do this whenever you want to change the subject of the camera --
currentSubject = ((currentSubject + 1) % 3) + 1 -- 1, 2, 3, 1, 2, 3, 1...

You probably want to add the % 3 at the end to make sure that it loops back to zero once it reaches 3.

Be aware that it may be difficult to get the exact order of the list if you use strings as indices. I believe the built in iterator functions iterate over it in alphabetical order rather than the order you put them in your script. You might want to also assign an id to each index in addition to your cameraRegions. You can do this by replacing each value with:

{
   ID = 1;
   Region = cameraRegions.PoliceOfficer;
}

And remember to change the ID for each one

Make a variable that holds the current index of the camera they’re viewing.

After adding one to the current index, if it is greater than the total number of cameras in the table, you’ll want to reset it back to the first index.