Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to get a variable from a table?

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,
	}
}

1 comment

0

print(nn.Foxy_Lover213.Enabled)

2 answers

✓ Accepted answer

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.

1 comment

0

in line four the userid is not defined

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

5 comments

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

0

This method works, It's the table it's running through that isn't properly giving the correct information

0

The value it prints is v, the the example you gave above prints the first value of nn.FoxyLover

0

you could do this

0

I updated it

This discussion is preserved as it was. New answers, comments, and votes are closed.