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)