#How to make it so players cant buy the same item after they leave and join

1 messages · Page 1 of 1 (latest)

full cliff
#

Ive got a shop script and I am wondering how I could make sure the player cant buy the same item twice as when they leave and join the ui doesnt save here is the script

errant furnace
#

Your best option here is to use DataStoreService. It’s the easiest way to save data. All you’ll really need to do is create a table that save a boolean that dictates whether or not the player has already bought the item, and save that boolean/upload it again when they rejoin. You can then check this boolean to see if the player owns the item later when they go to buy it.

full cliff
#

i already am using a datastore service

#

local DataStoreService = game:GetService("DataStoreService")
local shovelStore = DataStoreService:GetDataStore("ShovelPurchases")

game.Players.PlayerAdded:Connect(function(player)
local userId = tostring(player.UserId)

local data
local success, err = pcall(function()
    data = shovelStore:GetAsync(userId)
end)

if not success then
    warn("Failed to load data:", err)
    return
end

-- Create a default if no data
if not data then
    data = {
        hasRusty = false,
        hasBasic = false,
        hasIron = false,
    }
end

-- Store in player for session
local folder = Instance.new("Folder")
folder.Name = "ShovelData"
folder.Parent = player

for k, v in pairs(data) do
    local boolVal = Instance.new("BoolValue")
    boolVal.Name = k
    boolVal.Value = v
    boolVal.Parent = folder
end

end)

game.Players.PlayerRemoving:Connect(function(player)
local userId = tostring(player.UserId)
local folder = player:FindFirstChild("ShovelData")

if folder then
    local data = {}
    for _, child in ipairs(folder:GetChildren()) do
        data[child.Name] = child.Value
    end

    pcall(function()
        shovelStore:SetAsync(userId, data)
    end)
end

end)

errant furnace
#

Oh I see, sorry I missed that initially.

#

Are you simply not seeing your data load? Or is there an error message printing?

gray copper
#

I would strongly suggest rereading it to find the issue because I and other people like myself don't really feel like reading allat

full cliff
#

and im just wondering how i could do that