Problem with my data store and shop

Hey,
I don’t understand data store, so I rode some articles and saw some videos about. I’m trying to make a shop and to save 3 values:
-Win (As it’s called the number of player’s win)
-Money (Player’s money)
-Medicine (It’s the number of player has medicine (something to take some health))

So here is the script which creat these value

local DSS = game:GetService("DataStoreService")

local PlayerCash = DSS:GetDataStore("Cash")

 game.Players.PlayerAdded:connect(function(player)
player.CharacterAdded:Connect(function(Character)

local folder2 = Instance.new("Folder")
folder2.Parent = player
folder2.Name = "Settings"
local Playing = Instance.new("BoolValue",folder2)
Playing.Name = "Playing"
Playing.Value = false
local Money = Instance.new("NumberValue",folder2)
Money.Name = "Money"
local Win = Instance.new("NumberValue",folder2)
Win.Name = "Win"
local Medicine = Instance.new("NumberValue",folder2)
Medicine.Name = "Medicine"
Win.Value = PlayerCash:GetAsync(player.userId) or 0
Money.Value = PlayerCash:GetAsync(player.userId) or 0
Medicine.Value = PlayerCash:GetAsync(player.userId) or 0

game.Players.PlayerRemoving:connect(function(Player)
PlayerCash:SetAsync(Player.userId, Money.Value, Win.Value, Medicine.Value)
	end)
end)

end)

For my shop, it’s similar to mining simulator and there is a two button: you can buy with Robux and with Money. Then the value of Medicine increase.
Another gamepass can be only buy with Robux. When it’s buy, the button’s text will be “Owned”. But anything work.

Here is the script for Medicine buy button with Robux:

local GamePassID = 688347528
local plr = game.Players.LocalPlayer
local button = script.Parent
local MarkerplaceService = game:GetService("MarketplaceService")

script.Parent.MouseButton1Click:Connect(function()
print(0)
MarkerplaceService:PromptProductPurchase(plr, GamePassID)
end)

   game:GetService("MarketplaceService").PromptProductPurchaseFinished:Connect(function(plr,ido,purchased)
if purchased and ido == GamePassID then
    plr.Settings.Medicine.Value = plr.Settings.Medicine.Value + 1
end
 end)

Edit: When I buy for test it print this " [Players.mist0s.PlayerGui.Shop.Frame.Buy0.LocalScript:14: attempt to index local ‘plr’ (a number value)"

The script to buy with money:

    local player = game.Players.LocalPlayer
script.Parent.MouseButton1Click:Connect(function()
  if player.Settings.Money.Value >= 750 then
    player.Setting.Money.Value = player.Setting.Money.Value - 750
    player.Settings.Medicine.Value = player.Settings.Medicine.Value + 1
   end
end)

and the script for buy gamepass

 local DoubleID = 7050317
 local plr = game.Players.LocalPlayer
 local button = script.Parent
 local MarkerplaceService = game:GetService("MarketplaceService")

script.Parent.MouseButton1Click:Connect(function()
print(1)
    MarkerplaceService:PromptGamePassPurchase(plr, DoubleID)
end)

 game:GetService("MarketplaceService").PromptGamePassPurchaseFinished:Connect(function(plr,ido,   purchased)
if purchased and ido == DoubleID then
    script.Parent.Text = "Owned"
    end
 end)

game.Players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:connect(function(char)
	if game:GetService("MarketplaceService"):UserOwnsGamePassAsync(game.Players[char.Name].UserId, DoubleID) then
	   script.Parent.Text = "Owned"
	end
end)
 end)
1 Like

Before you edited the SaveAsync out, I saw you put them all in there in a table, so just do this

local stuff = PlayerCash:GetAsync(player.userId)
Win.Value = stuff[2] or 0
Money.Value = stuff[1] or 0 
Medicine.Value = stuff[3] or 0 

The order you are saving them into the table when the player leaves;

Money.Value, Win.Value, Medicine.Value

Money is 1 in the table
Win is 2 in the table
And Medicine is 3 in the table

So you are basically just finding them in the table!

Let me know if this worked, or you need any more help :wink:

1 Like

Sorry I don’t really understand, I have to put like that ?
local DSS = game:GetService(“DataStoreService”)

local PlayerCash = DSS:GetDataStore(“Cash”)

game.Players.PlayerAdded:connect(function(player)
player.CharacterAdded:Connect(function(Character)

local folder2 = Instance.new("Folder")
folder2.Parent = player
folder2.Name = "Settings"
local Playing = Instance.new("BoolValue",folder2)
Playing.Name = "Playing"
Playing.Value = false
local Money = Instance.new("NumberValue",folder2)
Money.Name = "Money"
local Win = Instance.new("NumberValue",folder2)
Win.Name = "Win"
local Medicine = Instance.new("NumberValue",folder2)
Win.Name = "Medicine"
Win.Value = PlayerCash:GetAsync(player.userId) or 0
Money.Value = PlayerCash:GetAsync(player.userId) or 0
Medicine.Value = PlayerCash:GetAsync(player.userId) or 0

game.Players.PlayerRemoving:connect(function(Player)
		local stuff = PlayerCash:GetAsync(player.userId)
      Win.Value = stuff[2] or 0
      Money.Value = stuff[1] or 0 
      Medicine.Value = stuff[3] or 0 
	end)
end)

end)

Well, for one, I’ve noticed this little oopsie:

Don’t think you meant to do that.

Also, please format your posts so that the code is readable and properly highlighted!

Well, I’m dumb lmao, and okk I will do that :wink:

You’re not dumb. Everyone makes those mistakes!

I once got a ton of bug reports only to realize that I’d had the wrong variable because of a copy-paste screw up just like this one. As long as you learn from your mistakes, making them isn’t dumb.

You have right, but it’s really frustrating