How do I add data to a datastore

Hello,
In this shop you can buy from the shop and the result should show in your inventory. It works as intended but when you leave it only saves the last item you bought. I want it to just add to your inventory. Thanks for any help.

Server Script

local ds = game:GetService("DataStoreService")

local database = ds:GetDataStore("Inventory")
local inventoryData = {}

local buyItemRM = game.ReplicatedStorage.buyItem
local updInventory = game.ReplicatedStorage.updateInventory

game.Players.PlayerAdded:Connect(function(plr)
	local success = nil
	local playerData = nil
	local attempt = 1
	
	repeat
		success, playerData = pcall(function()
			return database:GetAsync(plr.UserId) -- Assign data to player userid
		end)

		attempt += 1
	until success or attempt == 5
	
	if success then
		if playerData then
			updInventory:FireClient(plr, playerData)
		else
			updInventory = {
				"Testing"
			}
		end
		
	else 
		warn("DID NOT SAVE")
	end
	
end)




game.Players.PlayerRemoving:Connect(function(player)
	if inventoryData[player.UserId] then
		local success = nil
		local errorMessage = nil
		local attempt = 1
repeat
	success, errorMessage = pcall(function()
				database:SetAsync(player.UserId, inventoryData[player.UserId]) -- Assign data to player userid
	end)
	
	attempt += 1
until success or attempt == 5
		if success then
			print("Data Saved For".. player)
		else 
				warn("DID NOT SAVE")
		end
	end
end)





buyItemRM.OnServerEvent:Connect(function(plr, characther)
	print(characther)
	database:SetAsync(plr.UserId, characther)
	updInventory:FireClient(plr, database:GetAsync(plr.UserId))
end)


Shop Local script

local buyItemRM = game.ReplicatedStorage.buyItem

for i, v in script.Parent:GetChildren() do
	if v:IsA("TextButton") then
		v.Text = v:FindFirstChildWhichIsA("Model").CharacterName.Value
		v.Activated:Connect(function()
			
			for i, v in script.Parent.Parent.ViewportFrame.WorldModel:GetChildren() do
				v:Destroy()
			end
			local itemClone = v:FindFirstChildWhichIsA("Model"):Clone()
			itemClone.HumanoidRootPart.Position = Vector3.new(0,0,0)
			itemClone.Parent = script.Parent.Parent.ViewportFrame.WorldModel
			itemClone.HumanoidRootPart.Rotation = Vector3.new(0,0,90)
			print("Ran")
			print(script.Parent.Parent.ViewportFrame:GetChildren())
		end)
	end
end

script.Parent.Parent.Buy.Activated:Connect(function()
	print(script.Parent.Parent.ViewportFrame.WorldModel:GetChildren())
	buyItemRM:FireServer(script.Parent.Parent.ViewportFrame.WorldModel.Char.CharacterName.Value)
end)


Inventory Local Script


local updInventory = game.ReplicatedStorage.updateInventory


for i, v in script.Parent:GetChildren() do
	if v:IsA("TextButton") then
		v.Text = v:FindFirstChildWhichIsA("Model").Name -- Sets the text to the name of the model inside of the button
		v.Activated:Connect(function()
			for i, v in script.Parent.Parent.ViewportFrame.WorldModel:GetChildren() do
				v:Destroy()
			end
			local itemClone = v:FindFirstChildWhichIsA("Model"):Clone()
			itemClone.HumanoidRootPart.Position = Vector3.new(0,0,0)
			itemClone.Parent = script.Parent.Parent.ViewportFrame.WorldModel
			itemClone.HumanoidRootPart.Rotation = Vector3.new(0,0,90)
			print("Ran")
			print(script.Parent.Parent.ViewportFrame:GetChildren())
		end)
	end
end

updInventory.OnClientEvent:Connect(function(newItem)
		local newTextButton = Instance.new("TextButton")
		newTextButton.Parent = script.Parent
		newTextButton.Text = newItem
end)


Hi there buddy!
I have read thoroughly your issue.

I just need to ask couple of questions so I can figure out a best solution for you.

What do you mean of 'add data to a datastore? What were you trying to tell the script to?

Modify the server script so that it appends new items to the player’s existing inventory instead of overwriting it. Maybe use table.insert().

Instead of just saving the last value which I think its this line of code that’s doing it

database:SetAsync(player.UserId, inventoryData[player.UserId])

It should add the value to the datastore but im not sure how do do that when it SETS the data with setasync

I tried doing that but I kept getting an error saying

attempt to call missing method ‘FireClient’ of table

I don’t get that error since modifying the code to this

I’m thinking something like this… but I can’t test anything here so…

buyItemRM.OnServerEvent:Connect(function(plr, newItem)
	if not inventoryData[plr.UserId] then
		inventoryData[plr.UserId] = {}
	end
	table.insert(inventoryData[plr.UserId], newItem)
	database:SetAsync(plr.UserId, inventoryData[plr.UserId])
	updInventory:FireClient(plr, newItem)
end)
1 Like

This code didn’t directly work but I made a little modification and came up with this solution

Server Script

buyItemRM.OnServerEvent:Connect(function(plr, newItem)
	local inventoryTable = database:GetAsync(plr.UserId)
	table.insert(inventoryTable, newItem)
	database:SetAsync(plr.UserId, inventoryTable)
	updInventory:FireClient(plr, inventoryTable)
end)

Inventory Local Script

updInventory.OnClientEvent:Connect(function(inventoryTable)
	-- The first for loop deletes the existing buttons in the scrolling frame so they don't have double the items when the inventory updates
	
	for i, v in script.Parent:GetChildren() do
		if v:IsA("TextButton") then
			v:Destroy()
		end
		
	end
	
	-- creates a new button for each item in the inventory
		for i, v in inventoryTable do
			local newButton = Instance.new("TextButton")
			newButton.Parent = script.Parent
		end
end)

Figured it may be off a little bit. Glad you found your answer. Good luck with your game!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.