Cycling forwards and backwards in a table

Hello, I have a somewhat small question Today:

I have this script that’ll manage different “modes” a player can cycle through using Q and R. Q will move back and R will move forwards. The table has the name of all the modes while the order will indicate the order in which the player can switch to the next / previous mode.

How would I have the script know the current mode it’s on and move to the next / previous one if possible? I know that Table.Next exists but is there an inverse to this or is there a better way to approach this as a whole? Thanks for any help.

plr = game.Players.LocalPlayer
char = plr.Character or plr.CharacterAdded:Wait()
Sword = script.Parent.RiyetenSword
UIS  = game:GetService("UserInputService")

Mode = "None"

--Modes
Modes = {
	C1 = {Name = "Dreamcaster", Order = 1},
	C2 = {Name = "Fireclaw", Order = 2},
	C3 = {Name = "Guardian", Order = 3},
	C4 = {Name = "The Manual", Order = 4},
	C5 = {Name = "Console", Order = 5},
}

Sword.CanCollide = false
Sword.Parent = char
SW = Instance.new("WeldConstraint")
SW.Parent = char
SW.Part0 = char.Torso
SW.Part1 = Sword
Sword.Position = char.Torso.Position - Vector3.new(0,0,-0.5)

UIS.InputBegan:Connect(function(Key)
	if Key.KeyCode == Enum.KeyCode.Q then
		-- Stuff will happen here
	end
	
	if Key.KeyCode == Enum.KeyCode.R then
		-- Stuff will happen here
	end
end)

Could you store a value of your current index and just add or minus from it?

1 Like

Since there is an order to your table, I would recommend simply using an array, since the indexes are automatically numeric.

Ex: {"A", "B", "C"} = {[1] = "A", [2] = "B", [3] = "C"}

This way you can simply move around the table, like so:

local Modes = {
	{["Name"] = "Dreamcaster"}, --// Order is automatically 1
	{["Name"] = "Fireclaw"}, --// Order is automatically 2
	{["Name"] = "Guardian"}, --// Order is automatically 3
	{["Name"] = "The Manual"}, --// Order is automatically 4
	{["Name"] = "Console"}, --// Order is automatically 5
}

local function IterateTable(Table: table, StartingIndex: number, Increase: boolean)
	local MaximumIndex = #Table
	
	local NewIndex
	
	if Increase then
		if (StartingIndex + 1) > MaximumIndex then
			NewIndex = 1
		else
			NewIndex = StartingIndex + 1
		end
	else
		if (StartingIndex - 1) < 1 then
			NewIndex = MaximumIndex
		else
			NewIndex = StartingIndex - 1
		end
	end
	
	return Table[NewIndex]
end

IterateTable(Modes, 1, true) --> Returns {["Name"] = "Fireclaw"}
IterateTable(Modes, 4, true) --> Returns {["Name"] = "Console"}
IterateTable(Modes, 1, false) --> Returns {["Name"] = "Console"}
IterateTable(Modes, 3, false) --> Returns {["Name"] = "Fireclaw"}

I believe that this works after implementing it, thank you both for your awnsers! One thing though, How would I print out the name of the current mode since “Name” is no longer a variable (atleast how I see it.)

Extending from the code above, you could do

local Table1 = IterateTable(Modes, 1, true)
local Table2 = IterateTable(Modes, 4, true)
local Table3 = IterateTable(Modes, 1, false)
local Table4 = IterateTable(Modes, 3, false)

print(Table1["Name"]) --> "Fireclaw"
print(Table2["Name"]) --> "Console"
print(Table3["Name"]) --> "Console"
print(Table1["Name"]) --> "Fireclaw"
1 Like

Worked perfectly! Got a few errors but I fixed them myself, Thanks again!

Sorry to be a bother again, but after i’ve added the final stuff I’ve been trying to fix this issue when trying to print the names and I haven’t figured out why its happening.

Trying to print returns with nil

UIS.InputBegan:Connect(function(Key)
	if Key.KeyCode == Enum.KeyCode.E then
		local Final = IterateModes(Modes, CurrentMode.Value, true)
		print(Final["Name"])
	end
	
	if Key.KeyCode == Enum.KeyCode.Q then
		local Final = IterateModes(Modes, CurrentMode.Value, false)
		print(Final["Name"])
	end
end)

I basically tried to implement the answer provided but it has not worked. I’m assuming I need to return something but i’m unsure.

The cycle still works, but the script doesn’t know what the index is referring to (?)

Based on what you are saying that the script does not know what the index is referring to, that means the issue is probably coming from:

What have you set this to mean (as far as I can see, in any of the code snippets, CurrentMode was never defined nor is it clear that it has a Value index)? For the IterateTable/IterateMode function, the second argument should be a number, representing the current index in the array. The function is then set to return something.

Could you post the complete code that you are working with right now?

Ah, No worries, I resolved the issue by using a few values here and there, and works as intended, I’m still a bit wobbly on using tables and messed up somewhere. Thank you for clarifying, I’ll try to be more open minded before posting questions. Thanks and have a happy new year

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.