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 10 years ago

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

weapons = {"Shotgun","OrcBlade"}

for i,v in pairs(weapons) do

local DataStore = game:GetService("DataStoreService"):GetDataStore(v)

local key = newPlayer.userId..v

if DataStore:GetAsync(key) == true then
points.Value = points.Value+999
else
print("weapon not owned")
end
end

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 10 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