SetAsync don't work [SOLVED]

The Set Async doesn’t work, he don’t warn an error in Output, I don’t know what I did it wrong. Anyone can help me?

Script

local module = require(game.ServerStorage.Modules:WaitForChild('Status'))
local DSS = game:GetService('DataStoreService')
local StatusData = DSS:GetDataStore('asdasdasd')

game.Players.PlayerAdded:Connect(function(plr)
	-- [CREATING INSTANCES]
	local StatusF = Instance.new('Folder', plr)
	StatusF.Name = 'Status'
	
	module.CreateStatus('Strength', StatusF)
	module.CreateStatus('Defense', StatusF)
	module.CreateStatus('Stamina', StatusF)
	module.CreateStatus('Speed', StatusF)
	
	-- [STATUS LOAD]
	local s, cS = pcall(function()
		return StatusData:GetAsync(plr.UserId)
	end)
	
	print(cS)
	if s then
		if cS ~= nil then
			for _, v in pairs(StatusF:GetChildren()) do
				if v:IsA('IntValue') then
					print('Foi')
					v.Value = cS[v.Name]
				end
			end
		end
	else
		StatusData:SetAsync(plr.UserId, nil)
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	-- [PICK UP INSTANCES]
	local StatusF = plr.Status

	-- [STATUS SAVE]
	local T = module.PickStatus(StatusF)
	
	local s, e = pcall(function()
		if StatusData:GetAsync(plr.UserId) == nil then
			StatusData:SetAsync(plr.UserId, nil)
			StatusData:UpdateAsync(plr.UserId, T)
		else
			StatusData:UpdateAsync(plr.UserId, T)
		end
	end)
	
	if s then
		print('Data Saved')
	else
		warn(e)
	end
end)

Module Script

local module = {}

function module.CreateStatus(Name, Parent)
	local NewStatus = Instance.new('IntValue', Parent)
	NewStatus.Name = Name
	NewStatus.Value = 100
end

function module.PickStatus(folder)
	local t = {
		['Strength'] = folder.Strength.Value;
		['Speed'] = folder.Speed.Value;
		['Defense'] = folder.Defense.Value;
		['Stamina'] = folder.Stamina.Value;
	}
	return t
end

return module
2 Likes

DataModel:BindToClose(function) is relevant in this case. The game might have shut down and you were not able to save data for all the players presently in time?

Does this line return a function?

The second parameter for UpdateAsync is a function used to modify the data.
Also, don’t use SetAsync() and UpdateAsync() - use one or the other. UpdateAsync() is better for preventing data loss.

1 Like

More to use UpdateAsync, the data need to exists, I can’t use only UpdateAsync()

You can just check if it’s nil or not when comparing.

local newData = --new data here
DataStore:UpdateAsync(key, function(old)
    if old == nil then
        return newData
    end
    --comparison code here
    return newData
end)

Does module.PickStatus() return a function or not? If it doesn’t, it might cause an issue.

Yes, return, the Module Script is in the first message.

This is not returning a function, which is required for UpdateAsync().
Try this for UpdateAsync():

StatusData:UpdateAsync(plr.UserId, function(old)
    return T
end)

Here’s the documentation for UpdateAsync():
GlobalDataStore:UpdateAsync() documentation from Roblox Creator Hub

This worked, thank you very much.

1 Like

Thank you too, I using BindToClose now.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.