SetAsync not a valid member of DataStoreService

Hey there people, I’m trying to make a datastore that would save a player’s current disease, but whenever a player leaves, this error comes up:
image

I tried googling the problem up, but there was nothing that could help me.

Here’s the source of the script:

local DS = game:GetService("DataStoreService")
local diseaseDS = DS:GetDataStore("DiseaseDS")

local function saveData(player)
	
	local savedisease = player.Disease.Value
	
	local success, err = pcall(function()
		DS:SetAsync(player.UserId, savedisease)
	end)
	if success then 
		print("Disease "..tostring(player.Disease.Value).." has been saved")
	else
		print("Error whilst saving disease")
		warn(err)		
	end
end
game.Players.PlayerAdded:Connect(function(player)
	local Disease = Instance.new("StringValue")
	Disease.Name = "Disease"
	Disease.Parent = player
	local data
	local success, err = pcall(function()
		data = DS:GetAsync(player.UserId)
	end)
	if success and data then
		Disease.Value = data[1]
	else
		print("Player has no disease")
		Disease.Value = "none"
	end
end)
game.Players.PlayerRemoving:Connect(function(player)
	local success, err  = pcall(function()
		saveData(player)
	end)
	if success then
		print("Disease saved")
	else
		print("Error whilst saving disease")
	end
end)
game:BindToClose(function()
	for _, player in pairs(game.Players:GetPlayers()) do
		local success, err  = pcall(function()
			saveData(player)
		end)

		if success then
			print("Disease saved")
		else
			print("Error whilst saving disease")
		end
	end
end)```

cheers!
1 Like

you already have a pcall with save data so there is no need to use another pcall in the player removing and bindtoclose function

2 Likes

That ^ and you accidentally used your DS variable instead of diseaseDS

2 Likes

Call Datastore:SetAsync on diseaseDS, not DS.

The code would be diseaseDS:SetAsync()

The bottom two will error. Simply replace “DS” with “diseaseDS.”

2 Likes

have you tried using diseaseDS instead of DS?

1 Like