Data not saving

local DS = game:GetService("DataStoreService")
local Data = DS:GetDataStore("Save2")
local player
local ReplicatedStorage = game:GetService("ReplicatedStorage")
function e(plr,data)
	local folder = plr:WaitForChild("Tool")
	for i,v in pairs(ReplicatedStorage.Tools.Information:GetChildren())do
		local val = Instance.new("BoolValue",folder)
		val.Name = i.."Locked"
		if i == 1 then
			val.Value = true
		end
		if i == 2 then
			val.Value = true
		end
		local eb = Instance.new("BoolValue",folder)
		eb.Name = i.."Owned"
		if i == 1 then
			eb.Value = true
		end
		if data then
			for s,e in pairs(data)do
				if e.Name == i then
					val.Value = e.Value
					eb.Value = e.Value
				end
			end
		end
	end
end
game.Players.PlayerAdded:Connect(function(plr)
	local folder = Instance.new("Folder",plr)
	folder.Name = "Tool" 
	local Equipped = Instance.new("StringValue", folder)
	Equipped.Name = "Equipped"
	Equipped.Value = "1"
	local DataSaved = Data:GetAsync(plr.UserId.."Tool")	
	
	e(plr, DataSaved)
end)


game.Players.PlayerRemoving:Connect(function(plr)
	if plr.Tool then

		local Table = {}


		for i,v in pairs(plr.Tool:GetChildren())do
			local newitem = {Name = v.Name,Value = v.Value}
			table.insert(Table,newitem)
		end

		local save,erros = pcall(function()
			Data:SetAsync(plr.UserId.."Tool",Table)
			print("Code Saved")
		end)

	end
end)

Why won’t this save?

1 Like

Are there any errors or messages in the output regarding this script?

Also are you testing this in Studio? If so, have you enabled Studio API access from the settings on the website?

Is it always not saving or only some times?