How to make it so my shop saves (I already made inventory saving)

Hi there. I already made it so my tools save, but I can’t figure out how to make the shop save. For example, I buy a sword, it makes it so its already purchased after I buy it. I leave the game and rejoin. I have the sword but since I don’t have a data saving system for the shop I can still buy the sword again, so I have 2 swords. Please help.

Client:

local RS = game:GetService("ReplicatedStorage")
local PlayerS = game:GetService("Players")
local TweenS = game:GetService("TweenService")

local Coins = PlayerS.LocalPlayer.Coins

local ShopEvent = RS:WaitForChild("ShopEvent")
local AlreadyBought = RS:WaitForChild("AlreadyBoughtEvent")

local player = PlayerS.LocalPlayer
local bag = player.Backpack

local gui = script.Parent
local shop = gui.Shop
local desc = shop.ItemDescription
local toolSF = shop.ToolsScrollingFrame
local buyB = desc.BuyButton
local closeB = shop.CloseButtonF.CloseButton
local warningM = shop:WaitForChild("WarningM")
local errorAudio = warningM:WaitForChild("error")
local toolT = {}

local itemName = desc.ItemName
local itemCooldwn = desc.Cooldown
local itemDmg = desc.Damage
local itemPrice = desc.Price
local itemAbility = desc.Ability
local itemImg = desc.ItemImage

local selected = nil

local function warningFunction(warningmessage)
	local tween = TweenS:Create(warningM, TweenInfo.new(0.5), {TextSize = 45})
	
	warningM.Visible = true
	warningM.Text = warningmessage
	errorAudio:Play()
	tween:Play()
	
	task.wait(4)
	
		warningM.Visible = false
		warningM.TextSize = 20
		warningM.Text = ""
end

local function deleteValues()
	itemName.Text = "No Item Selected"
	itemCooldwn.Text = "Cooldown: N/A"
	itemDmg.Text = "Damage: N/A"
	itemPrice.Text =  "Price: N/A"
	itemAbility.Text = "Ability: N/A"
	itemImg.Image = ""
	
	buyB.Text = "Buy"
	buyB.BackgroundColor3 = Color3.fromRGB(19, 217, 19)
	buyB.TextColor3 = Color3.fromRGB(0, 0, 0)
end

local function alreadyBought(descendant)
	buyB.Text = "Already Bought"
	buyB.BackgroundColor3 = Color3.fromRGB(39, 16, 108)
	buyB.TextColor3 = Color3.fromRGB(255, 255, 255)
	descendant.BoughtPattern.Visible = true
end

local function buy(descendant)
	buyB.Text = "Buy"
	buyB.BackgroundColor3 = Color3.fromRGB(19, 217, 19)
	buyB.TextColor3 = Color3.fromRGB(0, 0, 0)
	descendant.BoughtPattern.Visible = false
end

table.insert(toolT, toolSF:GetChildren())

for i, descendant in pairs(toolSF:GetDescendants()) do
	if descendant:IsA("ImageButton") then
		descendant.MouseButton1Click:Connect(function()
			boughtVal = descendant.AlreadyBoughtVal
			if boughtVal.Value == true then
				alreadyBought(descendant)
			elseif boughtVal.Value == false then
				buy(descendant)
			end
			itemName.Text = descendant.ItemName.Value
			itemCooldwn.Text = "Cooldown: " .. descendant.Cooldown.Value
			itemDmg.Text = "Damage: ".. descendant.Damage.Value
			itemPrice.Text = "Price: $".. descendant.Price.Value
			itemAbility.Text = "Ability: ".. descendant.Ability.Value
			itemImg.Image = descendant.Image
			
			selected = descendant
		end)
	end
end

buyB.MouseButton1Click:Connect(function()
	if boughtVal.Value == false then
		if Coins.Value >= tonumber(selected.Price.Value) then
		ShopEvent:FireServer(selected)
		print(selected.ItemName.Value .. " has been fired")
			task.wait(1)
		elseif Coins.Value <= tonumber(selected.Price.Value) then
			warningFunction("You need " .. selected.Price.Value - Coins.Value .. " more coins")
			warn(player.Name .. " does not have enough coins!")
			task.wait(2)
		end 
	elseif boughtVal.Value == true then
		warningFunction("You already own the " .. selected.ItemName.Value .. "!")
		warn(selected.ItemName.Value .. " is already owned")
	end
end)

local function itemBought(alreadybought)
	buyB.BackgroundColor3 = Color3.fromRGB(39, 16, 108)
	buyB.TextColor3 = Color3.fromRGB(255, 255, 255)
	buyB.Text = "Already Bought"
	alreadybought.BoughtPattern.Visible = true
end

closeB.MouseButton1Click:Connect(deleteValues)
AlreadyBought.OnClientEvent:Connect(itemBought)

Server:

-- Services
local RS = game:GetService("ReplicatedStorage")
local PS = game:GetService("Players")
local SS = game:GetService("ServerStorage")
local DS = game:GetService("DataStoreService")
local ToolDataS = DS:GetDataStore("ToolData")

-- Remote Event
local ShopEvent = RS:WaitForChild("ShopEvent")
local AlreadyBought = RS:WaitForChild("AlreadyBoughtEvent")

-- Tools
local tools = SS:WaitForChild("Tools")
local toolTable = {}

game:GetService("Players").PlayerAdded:Connect(function(player)
	local toolData = ToolDataS:GetAsync(player.UserId)
	
	local Backpack = player:WaitForChild("Backpack")
	local StarterGear = player:WaitForChild("StarterGear")
	
	if toolData ~= nil then
		for i, v in pairs(toolData) do
			if tools:FindFirstChild(v) and Backpack:FindFirstChild(v) == nil and StarterGear:FindFirstChild(v) == nil then
				tools[v]:Clone().Parent = Backpack
				tools[v]:Clone().Parent = StarterGear
			end
		end
	end
	
	player.CharacterRemoving:Connect(function(char)
		char:WaitForChild("Humanoid"):UnequipTools()
	end)
end)

PS.PlayerRemoving:Connect(function(Player)
	for i, v in pairs(Player.Backpack:GetChildren()) do
		table.insert(toolTable, v.Name)
	end
	
	if toolTable ~= nil then
		ToolDataS:SetAsync(Player.UserId, toolTable)
	end
end)

function onEventRecieved(player, selected)
if selected.AlreadyBoughtVal.Value == false then
	local Coins = player.Coins
		if Coins.Value >= selected.Price.Value then
			Coins.Value -= selected.Price.Value
	
			local toolGiven = tools:FindFirstChildOfClass("Tool") and tools:FindFirstChild(selected.Name)
			print(toolGiven)
			local toolClone = toolGiven:Clone()
			
			toolClone.Parent = player.Backpack
			
			selected.AlreadyBoughtVal.Value = true
	
			print(selected.Name .. " has been bought")
			AlreadyBought:FireClient(player, selected)
			task.wait(1)
			
			elseif Coins.Value <= selected.Price.Value then
				warn("Not Enough Money")
			return
		end

	end
end
ShopEvent.OnServerEvent:Connect(onEventRecieved)

In that server side code, I don’t see anywhere that data is being saved or loaded, other than the tool data. No shop data in sight

1 Like

In order to prevent buying copies, you can check if the player owns the item before completing the purchase once the remote is fired server-side.

1 Like

I did make shop data but I deleted it after it didn’t work

I tried to do that but it didn’t work otherwise this wouldn’t be posted

Since the tool data store uses the folder called tools, how would I make the shop save since it uses a boolean Value to see if the tool is bought.

Yeah I made a few changes and now the player can’t buy the tool if they have a tool of the same name although it has a few flaws

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