So I'm trying to make a shop, and for the shop items I'm saving a data store key as a table of all the items, each of which are either true or false if they are already purchased or not. I have the following script in the button of a shop item:
button = script.Parent price = 738 player = button.Parent.Parent.Parent.Parent.Parent.Parent.Parent data = game:GetService("DataStoreService"):GetDataStore("MWTestData1") function buyItem() print(player.Name.." is attempting to buy "..button.Parent.Name) data:UpdateAsync(player.userId.."Inventory", function(old) if not old then old={arm=false} end --I'll add more items here as I create them, but for now I just have the arm as the only purchaseable item. if old.arm == false then print(player.Name.." hasn't bought the arm") if game.ServerStorage.PlayerStats[player.Name].Stats.Points.Value >= price then print(player.Name.." has enough points.") old.arm = true game.ServerStorage.PlayerStats[player.Name].Stats.Points.Value = game.ServerStorage.PlayerStats[player.Name].Stats.Points.Value - price print(player.Name.." purchased "..button.Parent.Name.."!") pointskey = player.userId.."Points" data:IncrementAsync(pointskey, -price) points = game.ServerStorage.PlayerStats[player.Name].Stats.Points points.Value = data:GetAsync(player.userId.."Points") print(player.Name.."'s points: "..points.Value) else print(player.Name.." doesn't have enough points") end else print("Arm already purchased") end print(old.arm) return old end) end button.MouseButton1Click:connect(buyItem)
I have two problems with this: First of all, and it's a smaller problem - this will let players buy the item if their points are above 0, not above the 738 (the price). Second, and this is the real problem I have. When I buy this item, everything works just fine. However, I can keep purchasing it over and over again until I'm out of points. Then, when I leave and come back, it has saved that I've purchased it, but only if I've bought it in the previous visit. I hope I've explained the problem alright, please let me know if you need more info.
--MightyWanderer
DataStore does have a limit to saves/loads based on the current number of players in the game's instance. I don't think your problem is the code, but a bug or a downside in the DataStoreService. Maybe it should have an object (for example a Model that holds all purchased items) as a temporary placeholder during that visit:
button = script.Parent price = 738 player = button.Parent.Parent.Parent.Parent.Parent.Parent.Parent local upgrades = player:findFirstChild("Upgrades") or Instance.new("Model", player) upgrades.Name = "Upgrades" data = game:GetService("DataStoreService"):GetDataStore("MWTestData1") function buyItem() print(player.Name.." is attempting to buy "..button.Parent.Name) data:UpdateAsync(player.userId.."Inventory", function(old) if not old then old={arm = false} end --I'll add more items here as I create them, but for now I just have the arm as the only purchaseable item. if old.arm == false and not upgrades:findFirstChild("Arm") then -- If it exists in the upgrades model placeholder, then it counts as being bought. print(player.Name.." hasn't bought the arm") if game.ServerStorage.PlayerStats[player.Name].Stats.Points.Value >= price then print(player.Name.." has enough points.") old.arm = true game.ServerStorage.PlayerStats[player.Name].Stats.Points.Value = game.ServerStorage.PlayerStats[player.Name].Stats.Points.Value - price print(player.Name.." purchased "..button.Parent.Name.."!") pointskey = player.userId.."Points" data:IncrementAsync(pointskey, -price) points = game.ServerStorage.PlayerStats[player.Name].Stats.Points points.Value = data:GetAsync(player.userId.."Points") print(player.Name.."'s points: "..points.Value) local up = Instance.new("Model", upgrades) -- Doesn't matter what it is. up.Name = "Arm" else print(player.Name.." doesn't have enough points") end else print("Arm already purchased") end print(old.arm) return old end) end button.MouseButton1Click:connect(buyItem)