The problem I'm facing is that everytime someone joins they start out with the base resource values, even after they played around a bit the values don't get saved (or loaded) correctly. Anyone knows what I'm missing?

function save(player,data)

	local datastore = game:GetService("DataStoreService"):GetDataStore("Data")
	for data,value in pairs(data) do
		value = player.HasTycoon:FindFirstChild(data).Value
		print("saving",data,value)
	end
		datastore:SetAsync(player.UserId,data)
		datastore = game:GetService("DataStoreService"):GetDataStore("Inventory")
		local inventory = {}
	for _,item in pairs(player:WaitForChild("Backpack"):GetChildren()) do
		if item:IsA("Tool") then
			table.insert(inventory,item.Name)
			print("saving",item.Name)
		end
	end
	datastore:SetAsync(player.UserId,inventory)
	print("Saved!")
end

game.Players.PlayerAdded:connect(function(player)--PLAYER JOINS
	local data={
	["Gold"]=75,
	["Wood"]=0,
	["Stone"]=0,
	["Wheat"]=0,
	["Valuables"]=0,
	["Title"]=0	
}
		local dataStore = game:GetService("DataStoreService"):GetDataStore("Data")
		local currentData =  dataStore:GetAsync(player.UserId)
		if not currentData then
			currentData = data
		end
		for data,value in pairs(currentData) do
		player.HasTycoon:FindFirstChild(data).Value = value
		print("Loading",data,value)
		end
	dataStore = game:GetService("DataStoreService"):GetDataStore("Inventory")
	local inventory = dataStore:GetAsync(player.UserId)
	if inventory then
		for _,v in pairs(inventory) do
			print(v)
			local item = game.ServerStorage.Blacksmith:FindFirstChild(v)
			if item then
				local item2 = item:Clone()
				item2.Parent = player.Backpack
			end
		end
	end
end)

--PLAYER LEAVES---------------
game.Players.PlayerRemoving:connect(function(player)
	
	local data={
	["Gold"]=75,
	["Wood"]=0,
	["Stone"]=0,
	["Wheat"]=0,
	["Valuables"]=0,
	["Title"]=0	
}
	save(player,data)
end)

2 comments

2

Why are you setting the data table as the defualt when the player leaves?

0

I thought that It'd work regardless because it gets changed inside the save() function, but it seems like that was the problem.