I want to figure out if their is a easy function or something other than looping through an entire dictionary in order to get a list of its keys/values. Their isn’t any significant issue if their isn’t one, but it would be convenient to know if their is one. When I tried looking for an answer to this, I found solutions to get all the keys being looping through it, but nothing on whether their was an easy way to getting them without having to make a loop for it (This question is just for clarification because I assume their isn’t one). A simple yes or no will suffice for an answer, but added details on upon a simple yes or no would be appreciative like if something like this has been requested to be added or something such as that. If a Roblox staff sees this and it isn’t to much to ask, I would like built-in functions to get a list of keys or a list of values from it if their isn’t already one.
I believe the only way to do this is loops, can I get more information on what you are trying to accomplish here since getting every key / value is equal to the table itself?
local exampleDictionary: { [string] : number } = {
["akeyone"] = 10,
["akeytwo"] = 20,
["akeythree"] = 30
}
for key: string, value: number in pairs(exampleDictionary) do
print(`The key is {key} and the value of the key is {value}`)
end
EDIT: Just read you wanted a way to get all the keys without iterating through it. I don’t think there is a way that exists, other than to just iterate through the dictionary with a loop.
I’m storing the player’s badges in their data store, so I can award badges while not going over the request rate limit. I’ll probably make a function myself that does this when I get over 10 badges cuz of the getplayerbadges is limited at that amount. I have a value which must be met in order to get the badge and the key to that is the badgeId, so I need the keys for the second parameter of getplayerbadges
Ah I see you are reaching GetAsync
rate limits? To prevent this I suggest you make one key for a player like "Player_" .. UserId
and store all data in there. However if you would like to list all keys inside a datastore you can use ListKeysAsync which returns pages of keys inside the datastore, beware that you can only get the key name, to get the value you would have to use :GetAsync()
on all provided keys.
I think that’s what I’m already doing or is what your suggesting different?
This is how I’m doing it and like I implied, I don’t have a function for the loop and what not atm, but this works, so like I said earlier not a major issue just a matter of potential convenience if their was a built-in function I did not know of
You said you were reaching Datastore rate limits, am I correct? Are you creating a different datastore for each player, if this is the case please do not do this. Instead create one datastore like “PlayerDatas” and save each players data inside that datastore as a Key
. You can use modules like ProfileService or the newer version ProfileStore which are great datastore management modules.
I’m not getting datastore rate limits, I’m getting badge request limits, I already fixed this with a timer, but I want it to be responsive instead of being awarded every minute if they reached the requirement. Also the way I’m doing it above will I get datastore rate limits from that if my game gets popular or something, so it’s better to save all player data to one thing or something?
Yes you will definetly get datastore limits from that so I would suggest using my method. GetAsync
and SetAsync
both have rate limits of 60 + (10 * number of players) per minute. As for badges, I do not understand your request. What do you mean by awarded? You are providing a using a check function on your script.
Wait, I’m confused how I could get datastore limits if it’s based off player base and what not, I feel like that isn’t correct since the amount of data requests that can be made is scaled with player amount. What I mean by awarded is just this badSer:AwardBadge(locPla.UserId,plaBad.cliBad0[badInd])
Yes, it does scale with player count but it can still be reached with populated lobbies, I suggest you use ProfileService and ProfileStore for this, however if you are worried that they will quickly build up to the 4MB key limit then you can continue with your method. As for award badge rate limits is 50 + 35 * number of players
so I suggest you save 35 - 40 (depending on max players) max every minute.
What are the conditions for the rate limit to be reached though is it rare for it to happen?
This depends on your operation, normally these are pretty high limits. If you are using API calls like this frequently I suggest adding pcall with a retry system (you can also block more API requests until the current one succeeds or add them to the que)
Would you give in an example?
A dictionary dont need loop to find anything really
Here is an example
local dict = {
Key1 = 12332
Key2 = 5432
}
print(dict.Key1) -- 12332
print(dict.Key2) --5432
There is another way where you can check if that dict has a property and has a value or it will return nil
local dict = {
Key1 = 12332
}
print(dict["Key1"]) --12332
print(dict["Key2"])---nil
No, I’m not trying to get values/keys in general, I’m trying to make a list that contains only the key values with a easy built-in function.
Well, I have not ran into any issues with this, but thanks for you input. I’ll look into this if I ever do run into this issue
there isn’t a built-in way to do that
but it’s possible to make a function that do that
local tableThatWeWantToGetTheKeysOrValuesFrom = {
["Hello"] = 1,
["Hello2"] = "Two",
["Hello3"] = 3
}
local function extractKeys(tbl)
local _tbl = {}
for k, _ in tbl do
table.insert(_tbl, k)
end
return _tbl
end
local function extractValues(tbl)
local _tbl = {}
for _, v in tbl do
table.insert(_tbl, v)
end
return _tbl
end
print("Keys")
print(extractKeys(tableThatWeWantToGetTheKeysOrValuesFrom))
print("Values")
print(extractValues(tableThatWeWantToGetTheKeysOrValuesFrom))