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

Database UpdateAsync and SetAsync not working?

I don't know what I've done wrong, I've played it in the real game and in the studio one and it won't work, I enabled api stuff in studio but it still doesn't work.

local ds = game:GetService("DataStoreService"):GetDataStore("Points")

game.Players.PlayerAdded:connect(function(player)
	-- datastore stuff
	game.ServerStorage.leaderstats:Clone().Parent = player
	local PointsValue = ds:GetAsync(player.UserId)
	if PointsValue then
		player.leaderstats.Points.Value = PointsValue else
		player.leaderstats.Points.Value = 0
	end
	
	while wait(5) do
		player.leaderstats.Points.Value = player.leaderstats.Points.Value + 300
	end
end)
		

game.Players.PlayerRemoving:connect(function(player)
	ds:UpdateAsync(player.UserId,player.leaderstats.Points.Value)
end)

3 comments

0

Please post your error.

0

As far I can see, you are using updateasync instead of setasync.

0

setasync will have cache sometimes and get async will get the wrong value, fixed it btw

1 answer

Hey, The problem was that in line 19 you are using UpdateAsync(), you should use SetAsync()

I fixed for you:

local ds = game:GetService("DataStoreService"):GetDataStore("Points")
	 
	game.Players.PlayerAdded:connect(function(player)
	    -- datastore stuff
	    game.ServerStorage.leaderstats:Clone().Parent = player
	    local PointsValue = ds:GetAsync(player.UserId)
	    if PointsValue then
	        player.leaderstats.Points.Value = PointsValue 
			else
	        player.leaderstats.Points.Value = 0
	    end
	 
	    while wait(5) do
	        player.leaderstats.Points.Value = player.leaderstats.Points.Value + 300
	    end
	end)
	 
	 
	game.Players.PlayerRemoving:connect(function(player)
	    ds:SetAsync(player.UserId,player.leaderstats.Points.Value)
	end)

Make sure you have enabled Studio Access to API Services

  1. Go to the following link: https://www.roblox.com/develop
  2. Click on the gear icon, choose "Configure Game"
  3. Check - Enable Studio Access to API Services

If you got any further problems please comment on this answer and I will do my best to help.

1 comment

0

Actually, sometimes SetAsync with cache but it doesn't matter anyways.

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