SetAsync does not work

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to achieve a DataStore system that duplicates a folder, parents it to replicated storage, sets data or gets the data.

  2. What is the issue? Include screenshots / videos if possible!
    However, a part of the script is not working which is the SetAsync part.
    I found out by using prints(), heres a image.
    image
    It does not print what it says after SetAsync.
    I have been looking at this for more than 30 minutes and still cannot find a solution, and it is apart of the player removing function,

game.Players.PlayerRemoving:Connect(function(plr)
	print(plr.Name .. "Left")
	local dataStore = dataStore_Service:GetDataStore(plr.UserId .. "Info")
	local zombieBio = game.ReplicatedStorage:FindFirstChild(plr.Name .. " Information")
	local info = zombieBio:GetDescendants()
	print("Got descendants")
	for index, descendant in pairs(info) do
		if descendant:IsA('StringValue') or descendant:IsA('BoolValue') or descendant:IsA('IntValue') then
			print(descendant.Name .. "Is descendant of" .. plr.Name)
			dataStore:SetAsync(descendant.Name, descendant.Value)
			print("set async")
		end
	end
	zombieBio:Destroy()
end)

This is my entire script which might not be too useful since I think this only has to do with the playerRemoving function.

local dataStore_Service = game:GetService("DataStoreService")

game.Players.PlayerAdded:Connect(function(plr)
	print(plr.Name .. "added")
	local dataStore = dataStore_Service:GetDataStore(plr.UserId .. "Info")
	local zombieBio = game.ReplicatedStorage.ZombieBio:Clone()
	zombieBio.Name = plr.Name .. " Information"
	zombieBio.Parent = game.ReplicatedStorage
	local info = zombieBio:GetDescendants()
	print("Got descendants")
	for index, descendant in pairs(info) do
		if descendant:IsA('StringValue') or descendant:IsA('BoolValue') or descendant:IsA('IntValue') then
			print(descendant.Name .. "Is descendant of" .. plr.Name)
			descendant.Value = dataStore:GetAsync(descendant.Name)
			print("got async")
		end
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	print(plr.Name .. "Left")
	local dataStore = dataStore_Service:GetDataStore(plr.UserId .. "Info")
	local zombieBio = game.ReplicatedStorage:FindFirstChild(plr.Name .. " Information")
	local info = zombieBio:GetDescendants()
	print("Got descendants")
	for index, descendant in pairs(info) do
		if descendant:IsA('StringValue') or descendant:IsA('BoolValue') or descendant:IsA('IntValue') then
			print(descendant.Name .. "Is descendant of" .. plr.Name)
			dataStore:SetAsync(descendant.Name, descendant.Value)
			print("set async")
		end
	end
	zombieBio:Destroy()
end)

I am not too experienced with DataStores so I have no idea why this does not work.

1 Like

I know about that, and my script makes sure to check that it is only a string, bool, or int value, which is in this part

if descendant:IsA('StringValue') or descendant:IsA('BoolValue') or descendant:IsA('IntValue') then
1 Like

And you are positive it doesn’t save? There’s a possibility that Studios is closing before it can save.

1 Like

Yeah, I wasn’t paying too much attention as I’m eating food, but Datastores work like this:

DataStore:SetAsync("key", data)

or you can use tables like this

local data = {
["Something"] = true,
["Something2"] = false
}
DataStore:SetAsync("key", data)

You keep on saving the descendant name as the key, which is how you get to the datastore section which it should be something special for each player like their userid and some string with it or just the userid. for what you’re doing, I recommend using a table like this:

 game.Players.PlayerRemoving:Connect(function(plr)
	print(plr.Name .. "Left")
	local dataStore = dataStore_Service:GetDataStore(plr.UserId .. "Info")
	local zombieBio = game.ReplicatedStorage:FindFirstChild(plr.Name .. " Information")
	local info = zombieBio:GetDescendants()
	print("Got descendants")
	local data = {}
	for index, descendant in pairs(info) do
		if descendant:IsA('StringValue') or descendant:IsA('BoolValue') or descendant:IsA('IntValue') then
			print(descendant.Name .. "Is descendant of" .. plr.Name)
			table.insert(data, descendant.Name)
			data[descendant.Name] = descendant.Value
		end
	end
	dataStore:SetAsync(plr.UserId.."Info", data)
	zombieBio:Destroy()
end)

this seems better for what you are trying to do and won’t create random datastores you don’t use. Also, studio doesn’t save with PlayerRemoving and uses BindToClose instead. Try in game.

1 Like

Make sure you’re not testing from Studio, PlayerRemoving event tends to not work on studio.

2 Likes