DataStore2 crash: "script that implemented this callback has been destroyed"

I’m trying to use the DataStore2 module to save the player’s items in my tycoon but after I click “stop” in Studio I meet the error in the title.

At the moment I’m trying only the saving feature of the tycoon(saving of cash is commented). I am trying to save in a dictionary.

local DataStore2 = require(script.Parent.DataStore2)
 DataStoreName = "ANameForThisPurpose"

DataStore2.Combine(DataStoreName, "Tycoon", "CashSave") --What is saved

game.Players.PlayerAdded:Connect(function(player)
	print(player.Name)
end)
game:BindToClose(function()
	-- triggers at shutdown
	if game:GetService("RunService"):IsStudio() then
		return
	end
end)
function Purchase(tbl)
	local userTycoon = DataStore2("Tycoon",script.Parent.Owner.Value):Get({})
	print("ok5")
		local cost = tbl[1]
		local item = tbl[2]
		local stats = tbl[3]
	if not script.Parent.PurchasedObjects:FindFirstChild(item.Object.Value) then
		print("ok6")
			stats.Value = stats.Value - cost
		--DataStore2("CashSave",script.Parent.Owner.Value):Set(stats.Value)
		print("ok7")
	end
	
	Objects[item.Object.Value].Parent = script.Parent.PurchasedObjects
	userTycoon[item.Object.Value] = true         
	DataStore2("Tycoon",script.Parent.Owner.Value):Set(userTycoon)
	print("ok8")
1 Like

Does this happen in-game rather than testing it on studio?

Like @WooleyWool said, I recommend trying it in-game.

I tried it in game and it didn’t save

I worked with a friend on it for about 5 hours (he implemented it in the same way on his tycoon) and both of us couldn’t find a solution to the problem.

I think I might try to learn ProfileService to do the saving process, but it’s been hard enough to understand DS2.