Hi, i am unsure if it is possible to use the updateasync API in Datastore with Lua Tables in order to save data. In this scenario, i created a textlabel which represent the value of coins which the player have, and also a intvalue inside it, however i would like to use updateasync to save multiple values in a table instead of SetAsync to ensure security. If so, how would i called the key which is inside the table? Thanks.
local datastore = game:GetService("DataStoreService"):GetDataStore("LuaTestData") game.Players.PlayerAdded:connect(function(plr) local screen = game.Lighting.ScreenGui:clone() screen.Parent = plr.PlayerGui local bitstat = screen.Bitstat local bitvalue = bitstat.Bitvalue local userkey = "User-" .. plr.UserId local finduser = datastore:GetAsync(userkey) if finduser then local savevalue = {finduser + 20,0} datastore:SetAsync(userkey,savevalue) bitvalue.Value = finduser[1] print("success1") else datastore:UpdateAsync(userkey, function(oldvalue) local newvalue = oldvalue or 0 newvalue = newvalue + 20 return newvalue end) bitvalue.Value = finduser[1] print("success2") end game.Players.PlayerRemoving:connect(function(plr) local userkey = "User-" .. plr.UserId datastore:UpdateAsync(userkey, function(savevalue) local newvalue = savevalue return newvalue end) end)
Because your code is ugly and messy, I'm going to go against all of my morals and just give you a straight-up fix. If you need it explaining, I can explain it to you. But you'll need to ask.
local datastore = game:GetService("DataStoreService"):GetDataStore("LuaTestData") game.Players.PlayerAdded:connect(function(plr) local screen = game.Lighting.ScreenGui:clone() -- Don't use Lighting. Please. screen.Parent = plr.PlayerGui local bitstat = screen.Bitstat local bitvalue = bitstat.Bitvalue local userkey = "User-" .. plr.UserId local finduser = datastore:GetAsync(userkey) -- FindUser is a table? if finduser then local savevalue = {finduser[1] + 20,0} -- I disagree with creating a new table but so be it. datastore:SetAsync(userkey,savevalue) bitvalue.Value = finduser[1] print("success1") else datastore:SetAsync(userkey, {20,0}); -- No point in UpdateAsync here -- You're only running this bit if finduser doesn't exist, in which case there's no point in assuming it might. bitvalue.Value = 20 -- Constant, definite, immutable: You just set it. print("success2") end end) -- You were missing an end here game.Players.PlayerRemoving:connect(function(plr) local userkey = "User-" .. plr.UserId --[[ datastore:UpdateAsync(userkey, function(savevalue) local newvalue = savevalue return newvalue end) ]]-- Not sure what you wanted to accomplish here datastore:SetAsync(userkey, {plr.PlayerGui.ScreenGui.Bitstat.Bitvalue.Value ,0}) end)
Sidenote: I really hate your naming conventions