How to save custom shop purchases

Hello, I am thinking of making a custom shop where players can buy weapons, textures, etc. I know how to save data like levels, exp etcetera but I don’t know how I should save their purchase. Like should I add a boolean value to the player for each different weapon and set it to true whenever they have purchased it or is there an easier way to do this?

1 Like

I have made a script for thing thing.

local mps = game:GetService(“MarketplaceService”)

local gamepass_id = GamepassID
game.Players.PlayerAdded:Connect(function(plr)
if mps:UserOwnsGamePassAsync(plr.UserId, gamepass_id) then
game.ServerStorage.HandlessSegway:Clone().Parent = plr:WaitForChild(“Backpack”)
game.ServerStorage.HandlessSegway:Clone().Parent = plr:WaitForChild(“StarterGear”)

end

end)

game.ReplicatedStorage.Segway.OnServerEvent:Connect(function(plr)
game.ServerStorage.HandlessSegway:Clone().Parent = plr:WaitForChild(“Backpack”)
game.ServerStorage.HandlessSegway:Clone().Parent = plr:WaitForChild(“StarterGear”)
end)

I used it to make a segway. Basically it will clone the. Put the item in server storage.

This works only for weapons. (30 charss)

Oh, but what I meant with a custom shop is a in-game shop where you buy weapons with in-game money. I don’t know how to save that on the datastore.

Datastores cannot save things like Tools or objects. They can only save primitive values such as tables, numbers, strings, and booleans.

Thus, you have to be a little more creative when saving these things through datastores. There’s a little something people do known as “serialization”, where they use a primitive value to represent the item bought in a datastore.

I recommend assigning each buyable item a number or some type of id (numbers are naturally less demanding on space than strings). You can have a dictionary mapping the IDs to the actual items. When you save to the datastore, you serialize the item by storing its ID in the datastore instead. When the player rejoins the game, you can deserialize and find the item connected with that ID to give to the player.

This serialization method is applicable to both ordinary Roblox datastores and Kampfkarren’s DataStore2 module, which has its own serialization methods BeforeSave() and BeforeInitialGet().

1 Like

I had always thought of using strings, e.g. someone owned a skin named ‘Red,’ I would save it in a table of owned items with the value ‘Red,’ but I didn’t think of how much more efficient that method would be.