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:
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
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
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.
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)