I am creating a game where you can buy, sell, and trade limited items based off of their real prices on the Roblox website. I am currently trying to create a saving system that stores the RAP of the limited you bought so you can see how much the RAP changed since the last time you played and the amount of that limited that you own into a table that should look something like this:
{
[LimitedId] = {amountOwned = 4, lastRAP = 49156}
[LimitedId] = {amountOwned = 2, lastRAP = 42112}
}
The only problem is that when you are buying a limited that you’ve purchased previously instead of updating the already created values in the table it created a new limited ID which is a string and doesn’t update the values of either the int value or the string when you trying buying it once again, someone please help with this as I have been trying to fix this for the last 3 hours.
local player = script.Parent.Parent.Parent.Parent.Parent
local dss = game:GetService("DataStoreService")
local ownedLimitedsDS = dss:GetDataStore("limitedsDS1")
local screen = script.Parent
screen.Purchase.MouseButton1Click:Connect(function()
local key = player.UserId .. "saveTest10"
local dataToSave = ownedLimitedsDS:GetAsync(key) or {}
local limited = screen.currentLimited.Value -- Object value referencing the limiteds folder
local limitedId = tonumber(limited.Id.Value) -- Converts the limiteds Id from a string to an number
print(limited)
local function hasValue(tab, limitedId)
for i, value in ipairs(dataToSave) do
if value == limitedId then
return true
end
end
end
if player:WaitForChild("leaderstats"):WaitForChild("Robux").Value >= limited.RAP.Value then
local ownedLimitedCopies = 0
local iteminfo = {}
if hasValue(dataToSave, limitedId) then
print("Player owns 1+ copies of this limited")
dataToSave[limitedId].amountOwned = dataToSave[limitedId].amountOwned + 1
dataToSave[limitedId].lastRAP = limited.RAP.Value
else
print("Player has never purchased this limited before")
iteminfo = {
amountOwned = 1,
lastRAP = limited.RAP.Value}
table.insert(dataToSave, limitedId, iteminfo)
end
print(dataToSave)
local success, response = pcall(ownedLimitedsDS.SetAsync, ownedLimitedsDS, key, dataToSave)
if success then
print("Successfully saved owned limiteds for " .. player.Name)
else
print("Failed to save owned limiteds for " .. player.Name .. "(" .. response .. ")")
end
else -- Player cannot afford limited
print("Cannot afford limited")
end
end)