How would I let the script know what index i'm referring it to

Hello, I tried asking on an older post but I had not recieved a response since then so I’ll re-iterate my issue here since its a different issue anyway

So, I have this script which cycles through different “Modes” of a table. But, the way it’s written, I can’t think of a way to get the index and its properties out so I can reference them.

local Modes = {
	{["Name"] = "Dreamcaster"},
	{["Name"] = "Fireclaw"},
	{["Name"] = "Guardian"},
	{["Name"] = "The Manual"}, 
	{["Name"] = "Console"}, 
}

CurrentMode = 0
Form = Instance.new("IntValue")
Form.Parent = char

Sword.CanCollide = false
Sword.Parent = char
SW = Instance.new("WeldConstraint")
SW.Parent = char
SW.Part0 = char.Torso:FindFirstChild("BackProxy")
SW.Part1 = Sword
Sword.Position = char.Torso:FindFirstChild("BackProxy").Position
Sword.BlueGlow.Position = Sword.Position + Vector3.new(1.11,1,0)

function IterateModes(Table, StartingIndex, Increase)
	
	local Max = #Table
	print(Max)
	local NewMode
	
	if Increase then
		if (StartingIndex + 1) > Max then
			NewMode = 1
		else
			NewMode = StartingIndex + 1
		end
		CurrentMode = NewMode
	else
		if (StartingIndex - 1) < 1 then
			NewMode = Max
		else
			NewMode = StartingIndex - 1
		end
		CurrentMode = NewMode
	end
end

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

How would I get a specific index’s property? The cycle outputs a number from 1-5 but How would i compare this to my table’s index and get the properties of the index number that matches the current one gained from the cycle?

So it seems you are trying to access a property (e.g., “Name”) of a particular mode in the array of modes.
Assuming the Final variable is the index you are trying to get, you could access the property Name like so:

local name = Modes[Final].Name

or:

local name = Modes[Final]["Name"]

I don’t see a return value at the end of the function, though so if you don’t already have it in the actual code, you should return the proper index at the end of the function. If I understand correctly, I believe that would just be return NewMode

1 Like

Wow, I’ve been sat here for hours trying to figure this out and the solution was so simple, thanks for your help!

Also Instead of using the Final value (since it doesn’t return anything) I used the “CurrentMode” Variable that changes to the current cycle number anyway

Works like a charm, thanks again!

1 Like

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