Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to get a variable from a table?

Asked by
Vain_p 78
5 years ago

So I tried doing [1] but it just errors

--[[
    Script
--]]
function Crate(player,users)
    local nn = PlayersDataBase:GetAsync(player.UserId,users)
    PlayersDataBase:SetAsync(player.UserId,users)
    for i,v in pairs(nn.Foxy_Lover213) do
        print(v[1])
    end
end

Here's the table

Players = {
    ["5_zn"] = {
        Enabled = false,
        UserID = "",
        UserName = "",
        Rarity = 5,
        Amount = 0,
    },
    BOF = {
        Enabled = false,
        UserID = "",
        UserName = "BOF",
        Rarity = 5,
        Amount = 0,
    },
    ["Foxy_Lover213"] = {
        Enabled = false,
        UserID = "343563692",
        UserName = "Foxy_Lover213",
        Rarity = 1,
        Amount = 0,
    },
    ["Lord_Baconnn"] = {
        Enabled = false,
        UserID = "429533714",
        UserName = "Lord_Baconnn",
        Rarity = 1,
        Amount = 0,
    }
}
0
print(nn.Foxy_Lover213.Enabled) Amiaa16 3227 — 5y

2 answers

Log in to vote
1
Answered by 5 years ago

This might help:

function Crate(player,users,what_to_read)
    local nn = PlayersDataBase:GetAsync(player.UserId,users)
    PlayersDataBase:SetAsync(player.UserId,users)
    for i,v in pairs(PlayersDataBase.Foxy_Lover213) do
        if i == what_to_read then
            return v
        end
    end
end

for the third value, put what you want to extract. e.g.

Crate(nil, nil, "Amount")

returns 0 because the "Amount" value of Foxy_Lover213 is 0.

0
in line four the userid is not defined Vain_p 78 — 5y
Ad
Log in to vote
0
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

You're index the variable already, you're trying to ask whether the value is a table and the first object inside, that's a huge syntax issue, just to this instead

function Crate(player,users)
    local nn = PlayersDataBase:GetAsync(player.UserId,users)
    PlayersDataBase:SetAsync(player.UserId,users)
    for i,v in pairs(nn.Foxy_Lover213) do
        print(v, i)
    end
end
0
thats print Foxy_Lover213 nil 1 nil 2 nil 343563692 nil false nil I want it to print one value of the table an example is the value of amount Vain_p 78 — 5y
0
This method works, It's the table it's running through that isn't properly giving the correct information Ziffixture 6913 — 5y
0
The value it prints is v, the the example you gave above prints the first value of nn.FoxyLover Ziffixture 6913 — 5y
0
you could do this Ziffixture 6913 — 5y
0
I updated it Ziffixture 6913 — 5y

Answer this question