How do I get the first Value of a Dictionary?

Hello, so I i’m trying to set the text of a text label to the first item of a Dictionary here is the Dictionary:
This is beeing returned by printing the Dictionary:
image
And what I want to do is set the text of te text label to the first index of the Dictionary like [5] (sorry I’m bad at explaining) and here is what I did:

for i, v in pairs(pets) do -- pets is the Dictionary 
	local temp = script.Parent.BackGround:FindFirstChild('TemplateViewPort')
	local clonedTemp = temp:Clone()
	clonedTemp.Parent = script.Parent.BackGround
	clonedTemp.Name = v
	clonedTemp.Percentage.Text = i -- I want this to be the [5]
	clonedTemp.Visible = true
end

But i does not return what I want.
Thank you!

The dictionary part of a table has no notion of order, so your question doesn’t really make sense.

You could wrap each key-value pair in an array though, to get order since you’ll then have an array of dictionaries.

local pets = {
    { key = 5, value = "Radioactive Kitty" },
    { key = 10, value = "Radioactive Bunny" },
    { key = 20, value = "Radioactive Doggy" },
    { key = 25, value = "Radioactive Fox" },
    { key = 40, value = "Radioactive Mouse" }
}

Then when traversing the array you would do

for i, v in pairs(pets) do
	local temp = script.Parent.BackGround:FindFirstChild('TemplateViewPort')
	
	if not temp then
		continue
	end

	local clonedTemp = temp:Clone()
	clonedTemp.Parent = script.Parent.BackGround
	clonedTemp.Name = v.value
	clonedTemp.Percentage.Text = v.key -- I want this to be the [5]
	clonedTemp.Visible = true
end
2 Likes

You can use this statement:

pets[next(pets)] --> returns Radioactive Kitty

Next is used to find the first key in table. In this case, it will return whatever is the value of first key in array.

1 Like

But I’m filling the table like this:

for i, v in pairs(game.ReplicatedStorage.Pets:GetChildren()) do
	if module.pets[v.Name] then
		pets[game.ReplicatedStorage.Pets:FindFirstChild(v.Name):GetAttribute("Chance")] = v.Name
	end
end

Thank you but I don’t want it to return the radioactive kitty I want it to return the chance of getting it because that [5] is the chance of getting the pet.

So you can insert a new array.

table.insert(pets, { key = game.ReplicatedStorage.Pets:FindFirstChild(v.Name):GetAttribute("Chance"), value = v.Name })
1 Like

It does work but there is 1 little problem, I am sorting the table:

function Sorter(a, b)
	return a[1] > b[1]
end

table.sort(pets, Sorter)

This wont work if I sort it.

Do you want it to sort in descending order by the chance ?

1 Like

Yeah that’s exactly what I want.

Then it’s a simple fix to modify it to compare the chance.

local function Sorter(a, b)
    return a[1].key > b[1].key
end
1 Like

I get this error tho:
image
Here is my full code:

local player = game.Players.LocalPlayer
local egg = workspace.Eggs:FindFirstChild(script.Parent.Name)
local uis = game:GetService('UserInputService')
local CanDisplay = false
local module = require(workspace.Eggs:FindFirstChild(script.Parent.Name).Rarity)
local pets = {}
local RangedPets = {}
local rotationSpeed = 2

function viewport(frame, item)
	for i, v in pairs(frame:GetChildren()) do
		if v:IsA('Camera') or v:IsA('Model') then
			v:Destroy()
		end
	end
	local rotation = 0
	local camera = Instance.new('Camera', frame)
	item.Parent = frame
	for i, v in pairs(item:GetDescendants()) do
		if v:IsA('MeshPart') or v:IsA('Part') then
			v.Anchored = true
		end
	end
	frame.CurrentCamera = camera
	game:GetService('RunService').RenderStepped:Connect(function()
		pcall(function()
			local pos = item.PrimaryPart.CFrame * CFrame.Angles(0,math.rad(rotation),0) * CFrame.new(0,0,-5)
			pos = CFrame.new(pos.p, item.PrimaryPart.Position)
			camera.CFrame = pos
			rotation += rotationSpeed
		end)
	end)
end

for i, v in pairs(game.ReplicatedStorage.Pets:GetChildren()) do
	if module.pets[v.Name] then
		table.insert(pets, { key =  game.ReplicatedStorage.Pets:FindFirstChild(v.Name):GetAttribute("Chance"), value = v.Name})
	end
end

local function Sorter(a, b)
	return a[1].key > b[1].key
end

table.sort(pets, Sorter)

print(pets)
for i, v in pairs(pets) do
	local temp = script.Parent.BackGround:FindFirstChild('TemplateViewPort')

	if not temp then
		continue
	end

	local clonedTemp = temp:Clone()
	clonedTemp.Parent = script.Parent.BackGround
	clonedTemp.Name = v.value
	clonedTemp.Percentage.Text = v.key
	clonedTemp.Visible = true
end




game:GetService('RunService').RenderStepped:Connect(function()
	if player:DistanceFromCharacter(egg.Position) < 15 then
		script.Parent.Enabled = true
		CanDisplay = true
	else
		script.Parent.Enabled = false
		CanDisplay = false
	end
end)

uis.InputBegan:Connect(function(input)
	if CanDisplay == true then
		if input.KeyCode == Enum.KeyCode.E then
			game.ReplicatedStorage.Remotes.HatchServer:FireServer(egg.Name)
		end
	end
end)

Alright my bad, the indexing with 1 is unnecessary. a.key > b.key since a and b will be the arrays

1 Like

Thank you so much! It really helped!