Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to save purchased items from a shop with datastore?

Asked by
trecept 367 Moderation Voter
6 years ago

So currently I have a working shop where players can purchase stuff, and when they do a remote is fired to the server for which sword that was, then if the player has the money it will clone the tool from the serverstorage into the player's backpack. What I wanted to do is make it so these tools are saved and when a player joins the game automatically gives them their purchased tools.

My original idea was to have a bool value inside each tool in the serverstorage to check whether it had been purchased or not, but then I realised if I do that then when the value changes, the tool will show as bought for everyone? Sorry if that's confusing, just advice on how to begin creating this sort of system?

2 answers

Log in to vote
0
Answered by 6 years ago

Try this:

local ItemStore = game:GetService("DataStoreService"):GetDataStore("Items")
local Items = {plr.Backpack:FindFirstChild("")} --Add the name of the item and if you want to add more just do: , plr.Backpack:FindFirstChild("") and add the name of the other item

game.Players.PlayerAdded:Connect(function(plr)
    local vSave = ItemStore:GetAsync(PlayerKey)

    if vSave then
        Items = vSave[1]
    else
        ItemStore:SetAsync(PlayerKey, {Items})
    end 
end)

game.Players.PlayerRemoving:Connect(function(plr)
    local PlayerKey = "player-"..plr.UserId

    ItemStore:SetAsync(PlayerKey, {Items})
end)

Put this in a script, If it works please click accept answer.

--MajinBluee

0
Wouldn't everyone have the same items then? There's also better ways to do this. Dog2puppy 168 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

Check out this answer that I gave to another scripter. Hope it helps. Have a great day scripting!

Answer this question