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

Can you have a loop with GetAsync in it?

Asked by 11 years ago

I heard that it caches, but would that be specific to a certain value?

01weapons = {"Shotgun","OrcBlade"}
02 
03for i,v in pairs(weapons) do
04 
05local DataStore = game:GetService("DataStoreService"):GetDataStore(v)
06 
07local key = newPlayer.userId..v
08 
09if DataStore:GetAsync(key) == true then
10points.Value = points.Value+999
11else
12print("weapon not owned")
13end
14end

It adds 999 points, but not the other 999 and I have the OrcBlade save scripted so it saves as newPlayer.userId.."OrcBlade"

1 answer

Log in to vote
0
Answered by 11 years ago

Line 9 would be ~= nil, as :GetAsync returns a value, not a boolean. If there is no value for the key, it returns nil. Of course, if you set the key's value to false beforehand, don't change it.

As for your question, yes this would work. I assume you're using this to see if a player owns a certain weapon, and if so they are awarded a lot of points. However, keep in mind that :GetAsync() caches the returned value for about 10 seconds, as this may affect the script if you build off of it.

Ad

Answer this question