Having Troubles with Data Stores

Hey, I’m Mash I am new to roblox Scripting, I am making a game with my friend. And I followed a tutorial for a shop thing. The only problem is that the Data System is broken.

  1. What do you want to achieve? I am trying to make a Shop System, that kind of like Piggy. This was from a tutorial But I changed it a lot.

  2. What is the issue? My data wont save, plus it is setting my currencies to 0. (But it is set to 1000, if your data hasn’t been saved before it will be changed to 1000)

  3. What solutions have you tried so far? I have tried re ordering all the code, I have tried to re write the code. I have tried to change the Data Store, but it doesn’t work. (And Yes I do have the studio API Studio thing enabled)

I am not very good with Data Stores at the moment.

This is a Script inside SeverScriptService. There is a Remote Event Inside replicated storage called “Send Data”

local DataStoreService = game:GetService("DataStoreService")

local DataStore = DataStoreService:GetDataStore("DataStoreReal")
	
	-- List of all the Old Data Stores
	
	-- MyDataStore  Status: Not Being Used anymore, it was for testing
	-- DataStoreReal Status: Currently Being Used
	
game.Players.PlayerAdded:Connect(function(player)
	
	local meat = Instance.new("IntValue")
	meat.Name = "Meat"
	meat.Value = 1000
	meat.Parent = player
	
	local money = Instance.new("IntValue")
	money.Name = "Money"
	money.Value = 1000
	money.Parent = player
	
	local gunInventory = Instance.new("Folder")
	gunInventory.Name = "GunInventory"
	gunInventory.Parent = player
	
	local skinsInventory = Instance.new("Folder")
	skinsInventory.Name = "SkinInventory"
	skinsInventory.Parent = player
	
	local equippedGun  = Instance.new("StringValue")
	equippedGun.Name = "EquippedGun"
	equippedGun.Parent = player
	
	local equippedSkin = Instance.new("StringValue")
	equippedSkin.Name = "EquippedSkin"
	equippedSkin.Parent = player

local data
	
	local success, errorMsg = pcall(function()
	     data = DataStore:GetAsync(player.UserId)
	end)
	
	if data ~= nil then 
		
		-- Needs to load:
		
		if data.EquippedGun then
			equippedGun.Value = data.EquippedGun
		end
		
		if data.EquippedSkin then
			equippedSkin.Value = data.EquippedSkin
		end
		
		if data.Meat then
			meat.Value = data.meat
		end
		
		if data.Money then
			money.Value = data.money
		end
		
		if data.Skins then
			for i,v in pairs(data.Skins) do
				local val = Instance.new("StringValue")
				val.Name = v
				val.Parent = skinsInventory
			end
		end
		
		if data.Guns then
			for i,v in pairs(data.Guns) do
				local val = Instance.new("StringValue")
				val.Name = v
				val.Parent = gunInventory
			end
		end
	end
	
	game.ReplicatedStorage.SendData:FireClient(player,data)
	
end)

game.Players.PlayerRemoving:Connect(function(player)
	
	local data = {}  -- Meat, Money, Skins/guns they own all get saved in here
	
	data.Meat = player.Meat.Value
	data.Money = player.Money.Value
	
	data.EquippedSkin = player.EquippedSkin.Value
	data.EquippedGun = player.EquippedGun.Value
	
	data.Skins = {}
	data.Guns = {}
	
	for i,v in pairs(player.SkinInventory:GetChildren()) do
		table.insert(data.Skins,v.Name)
		print(v.Name)
	end
	
	for i,v in pairs(player.GunInventory:GetChildren()) do
		table.insert(data.Guns,v.Name)
		print(v.Name)
	end
	
	local success,errorMsg = pcall(function()
	DataStore:SetAsync(player.UserId,data)
	end)
	
	if success then
		print("Successfully Saved!")
	end
	
end)

This is a local script inside a GUI. (Down the bottom is where the Remote event is picked up)

	for i, v in pairs(data) do
		local frame = createFrame(v.Name,v.Cost.Value,v,skins.Folder)
		
		frame.Button.MouseButton1Click:Connect(function()
			local result = game.ReplicatedStorage.BuyItem:InvokeServer(v.Name,"skin")
			if result == "bought" then
				print("Bought Skin!")
				frame.Button.Image = greenTick
				frame.Cost.Text = "Owned"
				
			elseif result == "equipped" then
   
                for _, object in pairs(skins.Folder:GetChildren()) do
	                if object:IsA("Frame") and object:FindFirstChild("Cost") then
	                   if game.Players.LocalPlayer.SkinInventory:FindFirstChild(object.Name) then
		                    object.Cost.Text = "Owned"
		
                          end
                      end
                 end
    
	             frame.Button.Image = greenTick
                 frame.Cost.Text = "Equipped"

			end
			
		end)
		
	end
end

function addGuns(data)
	for i, v in pairs(data) do
		local frame = createFrame(v.Name,v.Cost.Value,v,guns.Folder)
		
		frame.Button.MouseButton1Click:Connect(function()
			local result = game.ReplicatedStorage.BuyItem:InvokeServer(v.Name,"gun")
			if result == "bought" then
				
				frame.Button.Image = greenTick
				frame.Cost.Text = "Owned"
				
			elseif result == "equipped" then
   
                for _, object in pairs(guns.Folder:GetChildren()) do
	                if object:IsA("Frame") and object:FindFirstChild("Cost") then
	                   if game.Players.LocalPlayer.GunInventory:FindFirstChild(object.Name) then
		                    object.Cost.Text = "Owned"
		
                          end
                      end
                 end
    
	             frame.Button.Image = greenTick
                 frame.Cost.Text = "Equipped"

			end
			
		end)
		
	end
end

game.ReplicatedStorage.SendData.OnClientEvent:Connect(function()
	addSkins(skinsData)
	addGuns(gunsData)
end)
1 Like

May I know what is “Piggy” to help you?

Do you mean in the script or?!

You said somethng about piggy store.

Its a Piggy type Store, Like the Popular game Piggy.

Your code is similar to mine and mine does not work either. Have you tried DataStore2? I was playing with it and it worked.

Never head of a game called: “Piggy”.

https://www.roblox.com/games/4623386862/Piggy-ALPHA-CHAPTER-12?refPageId=35176a8b-fe06-4fbe-9fc0-e24abbf93a78

That’s The Game. You can sew the shop, I am trying to make.

Wow, Thanks. I changed it to DataStore2 and it worked. Lol

1 Like

Knew it xD :+1: good luck with your project!

1 Like

Didn’t you ever see a game called ‘Piggy’ on the front page? It is a popular game.