Switching to DataStore2

Okay there are no more errors for now. Let me test them. Also do you want to help out with my game and be a scripter?

1 Like

I’m afraid I’m too busy with my own games at the moment, sorry.

Oh no its okay as a scripter you can just ocassionally help out but how would I contact you? Mainly probably every two weeks you have a small project or maybe once a month.

If you send me a friend request you could contact me through Roblox homechat.

kk gotcha will do taht right now

Let me test the scripts rn hold up

Ok, let me know if you have any trouble with the new scripts!

Still same problem and now the gold value is broken

image

Ok, I see what I did wrong with the gold, but it may take me a little while longer to fix the inventory.

okay gotcha I got an idea let me show you the old inventory script. It did not have this problem.

local DataStore = game:GetService("DataStoreService"):GetDataStore("MyDataStore")

game.Players.PlayerAdded:Connect(function(plr)
	
	
	
	
	local data
	
	local success, errorMessage = pcall(function()
		data = DataStore:GetAsync(plr.UserId)
	end)
	if data ~= nil then
		for _, toolName in pairs(data) do
			
			local tool = game.ReplicatedStorage.Tools:FindFirstChild(toolName)
			
			if tool then
				local newTool = tool:Clone()
				newTool.Parent = plr.Backpack
				
				local newTool = tool:Clone()
				newTool.Parent = plr.StarterGear
				
			end
		end
	end	
end)
game.Players.PlayerRemoving:Connect(function(plr)
	
	local toolsTable = {}
	
	for _, tool in pairs(plr.Backpack:GetChildren()) do
		if game.ReplicatedStorage.Tools:FindFirstChild(tool.Name) then
			table.insert(toolsTable,tool.Name)
		end
	end
	
	
	local success, errorMessage = pcall(function()
		DataStore:SetAsync(plr.UserId,toolsTable)
	end)
	
end)

game:BindToClose(function()
	for _, plr in pairs(game.Players:GetPlayers()) do
			local toolsTable = {}
			
			for _, tool in pairs(plr.Backpack:GetChildren()) do
				if game.ReplicatedStorage.Tools:FindFirstChild(tool.Name) then
					table.insert(toolsTable,tool.Name)
				end
			end
			
			
			local success, errorMessage = pcall(function()
				DataStore:SetAsync(plr.UserId,toolsTable)
			end)	
		end
end)

I think this should fix the inventories :smiley:

game.Players.PlayerAdded:Connect(function(player)
	local data
	local module = require(1936396537)
	local tools = module("tools", player)
	data = tools:Get({})

	if typeof(data) == "table" then
		for _, toolName in next, data do
			local tool = game.ReplicatedStorage.Tools:FindFirstChild(toolName)
			
			if player.Backpack:FindFirstChild(tool.Name) then
				return
			end
			
			if tool then
				local newTool = tool:clone()
				newTool.Parent = player.Backpack

				local newTool = tool:clone()
				newTool.Parent = player.StarterGear
			end
		end
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local toolsTable = {}

	local module = require(1936396537)
	local tools = module("tools", player)

	for _, tool in next, player.Backpack:children() do
		if game.ReplicatedStorage.Tools:FindFirstChild(tool.Name) then
			local newTable = tools:Get()
			table.insert(newTable, tool.Name)
			tools:Set(newTable)
		end
	end
end)

K let me try right now hold up.

Yes the inventory is working right now.

And this the gold script, this time I’m confident that this will work:

local defaultValue: number = 100 --If player has no data then their gold will be set to this value

local DataStore2 = require(1936396537)
DataStore2.Combine("MasterKey", "Gold")

game.Players.PlayerAdded:Connect(function(player)
	local goldData = DataStore2("Gold", player)
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"

	local goldStat = Instance.new("IntValue", leaderstats)
	goldStat.Name = "Gold"

	local function UpdateStat()
		goldStat.Value = goldData:Get(defaultValue)
	end

	goldData:OnUpdate(function()
		UpdateStat()
	end)
	
	goldStat.Changed:Connect(function()
		if goldStat.Value > goldData:Get() then
			goldData:Set(goldStat.Value)
		end
	end)

	goldData:Increment(0,0)
end)

Okay let me try it right now. Thanks!


It is still the same because it only shows other’s gold

Maybe try this:

local defaultValue: number = 100 --If player has no data then their gold will be set to this value

local DataStore2 = require(1936396537)
DataStore2.Combine("MasterKey", "Gold")

game.Players.PlayerAdded:Connect(function(player)
	local goldData = DataStore2("Gold", player)
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"

	local goldStat = Instance.new("IntValue", leaderstats)
	goldStat.Name = "Gold"

	local function UpdateStat()
		goldStat.Value = goldData:Get(defaultValue)
	end

	goldData:OnUpdate(function()
		UpdateStat()
	end)
	
	goldStat.Changed:Connect(function()
		if goldStat.Value > goldData:Get() then
			goldData:Set(goldStat.Value)
		end
	end)

	--goldData:Increment(0,0)
end)