How to get to the next value in a table?

Heya every1!
I’m in a hurry right now, so this post isn’t good.
But how do i go to the next value in a table.
like this table

local Colors = {
[“Patience”] = BrickColor.new(“Toothpaste”),
[“Determination”] = BrickColor.new(“Really red”),
[“Kindness”] = BrickColor.new(“Lime green”),
[“Bravery”] = BrickColor.new(“Br. yellowish orange”),
[“Intergity”] = BrickColor.new(“Dark blue”),
[“Justice”] = BrickColor.new(“Brick yellow”),
[“Perseverance”] = BrickColor.new(“Royal purple”)
}
if i want to go from patience to the next value, is there any specific function like Table:Next() or something?
Thanks

With a loop you’re okay.

for i,v in pairs(Colors) do
print(i,v)
end

will try that thanks you sir :smiley:

im not home rn so ill try it l8er

If you are on Patience and what the next entry, you can use this.
next(Colors, “Patience”)
Dictionaries (In Vanilla Lua at least) don’t have a specified order though, so it may not give you exactly what you want. If that is the case, you will need to re-work it so that it uses numbers instead of strings for the keys.

2 Likes

Could you give me an example? I don’t really get it,
what you sent is just a normal for loop.

That is exactly what that is. I believe he is suggesting that you can integrate it with whatever you’re doing. Or maybe he misunderstood. Without knowing why you need it, it can be difficult to recommend the best method.

2 Likes

So, I’ve tried your method, and this is what happend

local Colors = {
	["Patience"] = BrickColor.new("Toothpaste"),
	["Determination"] = BrickColor.new("Really red"),
	["Kindness"] = BrickColor.new("Lime green"),
	["Bravery"] = BrickColor.new("Br. yellowish orange"),
	["Intergity"] = BrickColor.new("Dark blue"),
	["Justice"] = BrickColor.new("Brick yellow"),
	["Perseverance"] = BrickColor.new("Royal purple")
}

local currentTrait
local nextTrait
--Functions--
game:GetService("UserInputService").InputBegan:Connect(function(input, gp)
	if gp then return end
	if input.KeyCode == Enum.KeyCode.E then
		table.find(Colors,"Determination")
		print(Colors["Determination"])
		currentTrait = Colors["Determination"]
		nextTrait = next(Colors,currentTrait)
		currentTrait = nextTrait
		
	end
end)

And it gave me this error:

 20:40:25.593 - invalid key to 'next'

20:40:25.595 - Script 'Players.GreenSubZeroGamesYT.Backpack.Knife.LocalScript', Line 20
20:40:25.596 - Stack End

next takes a key, not a value. Colors[“Determination”] is a value, “Determination” is the key to access it.

game:GetService("UserInputService").InputBegan:Connect(function(input, gp)
	if gp then return end
	if input.KeyCode == Enum.KeyCode.E then
		table.find(Colors,"Determination")
		print(Colors["Determination"])
		currentTrait = "Determination"
		nextTrait, value = next(Colors,currentTrait)
-- so now nextTrait="Kindness", value=BrickColor.new("Lime green")
		currentTrait = nextTrait
		
	end
end)
2 Likes

thank you very much! Fixed my problem. Also learned something new.
Have a good night/day! :grin: