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)