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

UpdateAsync with Lua tables?

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)

3 comments

0

You're doing a lot of mixing of syntax for numbers with syntax for tables here, which type of value is finduser supposed to be?

0

Sorry if i am unclear on this question, but i am currently using SetAsync to save the lua table however i would like to use UpdateASync to save the data inside the table

0

I am also using a IntValue inside the textlabel which update the textlabel continuously

1 answer

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

5 comments

0

Sorry if my code is very messy but i am trying to accomplish that every time the datastore table get saved while bitstat only display it, not setting the datastore to the bitvalue

0

What? I swear you make less sense every time you say anything.

0

Ok, excuse me if my explanation is out of the world but the bitvalue inside the bitstat is changed everytime the datastore key changes,so it is only a display of the data to the player, while to change the value, i want to set the player's data directly not through a third party like bitvalue.

0

Technically i am tring to save a table like in SetAsync(userkey,{0,0)) with UpdateAsync

0

You still make 0 sense. If you keep talking, you could try for a record of going into negative levels of sense.

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