Find the position of a string in a table

I want to make a player’s moveset swap between movesets in a table by finding their current moveset, checking where that moveset is in a table of movesets, adding/subtracting 1 from that number, finding the moveset in the new position and giving them that moveset. The issue is that i have no idea how to do this, I have tried table.find but i don’t think that’s what i need

--Movesets that the class has
VergilModule.Movesets = {
	["1"] = "Yamato",
	["2"] = "Beowulf",
	["3"] = "Daggers"
}

--
SwapRemote.OnServerEvent:Connect(function(player: Player, Direction: string)
	local Character = player.Character or player.CharacterAdded:Wait()
	local Humanoid = Character:FindFirstChildOfClass("Humanoid")
	
	local ClassModule = LoadClassModule:FindModule(player:GetAttribute("Class"))
	if Direction == "Up" then
		

	elseif Direction == "Down" then
	

	end
end)
2 Likes

I think table.find doesn’t really work in your case because your movesets table isn’t a proper array, it’s more like a dictionary with string keys like "1" = "Yamato".

table.find only works when your table is structured like { "Yamato", "Beowulf", "Daggers" }, where the keys are actual numbers.

If you still want to keep the current structure, you can just loop through the table and compare values manually. For an example:

for key, value in pairs(Movesets) do
    if value == currentMoveset then
        local index = tonumber(key)
        -- then use the index however you want
        break
    end
end

So yeah, either change the table into a proper array or do a simple loop like that. That should work.

3 Likes