So if you don't understand what I'm trying to say, here is a better explanation: My datastore saves and loads perfectly fine, but when I leave, the datastore doesn't load in the next time I test the game

The datastore just gets the previous load file from the previous, previous (yes, two previous's) test and doesn't load the one that was recently saved.

I don't understand why this is happening...

Script: (yes i know it's long sorry)

local datastore = game:GetService("DataStoreService"):GetDataStore("BronzeDarkRPstats")
local dataTable = {}
function save(plr)
	dataTable.cash = plr.Cash.Value
	dataTable.karma = plr.Karma.Value
	dataTable.inventory = {}
	for i,item in ipairs(plr:WaitForChild("Inventory"):GetChildren()) do
		table.insert(dataTable.inventory,item.Name)
		print("saved "..item.Name.." into datastore")
	end
	
	local success,failure = pcall(function()
		datastore:SetAsync(plr.UserId,dataTable)
	end)
	if success then
		print('data saved')
	elseif failure and not success then
		print(failure)
		warn('data failed to save')
	end
	for i,v in pairs(workspace:GetDescendants()) do
		if v.Name == plr.Name.."_Item" then
			v:Destroy()
		end
	end
end
function load(plr)
	local data
	
	local success,failure = pcall(function()
		data = datastore:GetAsync(plr.UserId) --load
	end)
	
	if success and data then
		plr.Cash.Value = data.cash
		plr.Karma.Value = data.karma
		for i,v in ipairs(data.inventory) do
			local item = game.ReplicatedStorage.Items:FindFirstChild(v)
			if item then
				local clone = item:Clone()
				clone.Parent = plr.Inventory
				print('loaded '..item.Name)
			end
		end
		print("successfully loaded data")
	elseif failure and not success then
		print(failure)
		warn('data did not load')
	end
end

game:GetService("Players").PlayerAdded:Connect(function(plr)
	local Cash = Instance.new("IntValue")
	Cash.Parent = plr
	Cash.Name = 'Cash'
	Cash.Value = 10000
	local Karma = Instance.new("IntValue")
	Karma.Parent = plr
	Karma.Name = 'Karma'
	local printersPurchased = Instance.new("IntValue")
	printersPurchased.Parent = plr
	printersPurchased.Name = "PrintersPurchased"
	local job = Instance.new("StringValue")
	job.Parent = plr
	job.Name = "Job"
	local payment = Instance.new("IntValue")
	payment.Parent = plr
	payment.Name = "Payment"
	local Inventory = Instance.new("Folder")
	Inventory.Parent = plr
	Inventory.Name = "Inventory"
	local inventorySpaceTaken = Instance.new("IntValue")
	inventorySpaceTaken.Parent = plr
	inventorySpaceTaken.Name = "InventorySpaceTaken"
	
	plr.CharacterAdded:Connect(function(char)
		local flagged = Instance.new("BoolValue")
		flagged.Parent = char
		flagged.Name = "Flagged"
		flagged.Value = false
		local nametag = game.ReplicatedStorage.Misc.Nametag:Clone()
		nametag.Parent = char:WaitForChild("Head")
		nametag.Person.Text = plr.Name
		nametag.Job.Text = "["..job.Value.."]"
		job.Changed:Connect(function(val)
			nametag.Job.Text = "["..val.."]"
		end)
		flagged.Changed:Connect(function(val)
			if val == true then
				nametag.Person.TextColor3 = Color3.fromRGB(255,0,0)
			elseif val == false then
				nametag.Person.TextColor3 = Color3.fromRGB(255,255,255)
			end
		end)
	end)
	
	load(plr)
end)
game.Players.PlayerRemoving:Connect(save)
game:BindToClose(function()
	for _, Player in ipairs(game.Players:GetPlayers()) do
	   coroutine.wrap(save, Player);
	end
end)

I can't find anything wrong with this script, it's supposed to work normally and not just load up some previous data from before...

2 comments

0

The only problem I find is on line 99. You did not assign a value to the plr argument for your save() function.

0

@LoveSpeaksOutTruth No it's not a problem. There is already a built in argument in PlayerRemoving that is the player.