Trying to make a shop that will save datastore

I’m trying to make a shop that will save to datastore for instance when a player leaves, at the moment I’m having trouble just getting it to save and load properly at all. I don’t know what the issue is because it doesn’t really error anything, this is what it says in console:
image
I have been working on this for days and it still doesn’t work but I don’t even know what to look up that would help me. This is my first real game on roblox so sorry my code is messy.

Datastore Script (Also has leaderstats)

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetOrderedDataStore("WinDataStore")
local ItemDataStore = DataStoreService:GetOrderedDataStore("ItemDataStore")

local function updateLeaderboard()
	local success, errorMessage = pcall(function()
		local Data = DataStore:GetSortedAsync(false, 10)
		local WinsPage = Data:GetCurrentPage()
		for Rank, data in ipairs(WinsPage) do
			local userName = game.Players:GetNameFromUserIdAsync(tonumber(data.key))
			local Name = userName
			local Wins = data.value
			local isOnLeaderboard = false
			for i, v in pairs(game.Workspace.Leaderboard.LeaderboardGui.Holder:GetChildren()) do
				if v.Player.Text == Name then
					isOnLeaderboard = true
					break
				end
			end

			if Wins and isOnLeaderboard == false  then
				local newLbFrame = game.ReplicatedStorage:WaitForChild("LeaderboardFrame"):Clone()
				newLbFrame.Player.Text = Name
				newLbFrame.Wins.Text = Wins
				newLbFrame.Rank.Text = "#"..Rank
				newLbFrame.Position = UDim2.new(0, 0, newLbFrame.Position.Y.Scale + (.08 * #game.Workspace.Leaderboard.LeaderboardGui.Holder:GetChildren()), 0)
				newLbFrame.Parent = game.Workspace.Leaderboard.LeaderboardGui.Holder
			end
		end
	end)

	if not success then
		print(errorMessage)
	end
end

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local coins = Instance.new("IntValue")
	coins.Name = "Coins"
	coins.Value = 0
	coins.Parent = leaderstats

	local bool = Instance.new("BoolValue")
	bool.Name = "HasWon"
	bool.Parent = player
	
	local bool = Instance.new("BoolValue")
	bool.Name = "InGame"
	bool.Parent = player

	local playerUserId = player.UserId

	local data
	local success, errormessage = pcall(function()
		data = DataStore:GetAsync(playerUserId)
	end)

	if success then
		coins.Value = data
	end
	
	local itemdata
	local success1, errormessage1 = pcall(function()
		itemdata = ItemDataStore:GetAsync(playerUserId)
	end)
	local items = {0, 0, 0, 0}
	if success1 then
		print("Loaded Data")
		items = itemdata
		wait(1)
		if type(items) == "table" then
			print("found")
		else
			items = {0, 0, 0, 0}
		end
		player.PlayerGui.Shop.Shop.ItemHolder.ItemShop.Sword.Purchased.Value = items[1]
		player.PlayerGui.Shop.Shop.ItemHolder.ItemShop.Sword.Equipped.Value = items[2]
		player.PlayerGui.Shop.Shop.ItemHolder.ItemShop.BigSword.Purchased.Value = items[3]
		player.PlayerGui.Shop.Shop.ItemHolder.ItemShop.BigSword.Equipped.Value = items[4]
	end
	if not success1 then
		print(errormessage1)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local playerUserId = player.UserId

	local data = player.leaderstats.Coins.Value

	local success, errormessage = pcall(function()
		DataStore:SetAsync(playerUserId, data)
	end)

	if success then
		print("Saved")
	else 
		print("Error")
		warn(errormessage)
	end
end)

game.Players.PlayerAdded:Connect(function(player)
	wait(5)
	player.PlayerGui.Shop.Shop.ItemHolder.ItemShop.Sword.Purchased.Changed:Connect(function()
		print("Change")
		for _, player in pairs(game.Players:GetChildren()) do
			local playerUserId = player.UserId
			local items = {player.PlayerGui.Shop.Shop.ItemHolder.ItemShop.Sword.Purchased.Value, player.PlayerGui.Shop.Shop.ItemHolder.ItemShop.Sword.Equipped.Value, player.PlayerGui.Shop.Shop.ItemHolder.ItemShop.BigSword.Purchased.Value, player.PlayerGui.Shop.Shop.ItemHolder.ItemShop.BigSword.Equipped.Value}
			local itemdata = items

			for _, item in pairs(itemdata) do
				local success1, errormessage1 = pcall(function()
					ItemDataStore:SetAsync(playerUserId, item)
				end)
				if success1 then
					print("Saved Data")
				end

				if not success1 then
					print(errormessage1)
				end
			end
		end
	end)
	player.PlayerGui.Shop.Shop.ItemHolder.ItemShop.Sword.Equipped.Changed:Connect(function()
		print("Change")
		for _, player in pairs(game.Players:GetChildren()) do
			local playerUserId = player.UserId
			local items = {player.PlayerGui.Shop.Shop.ItemHolder.ItemShop.Sword.Purchased.Value, player.PlayerGui.Shop.Shop.ItemHolder.ItemShop.Sword.Equipped.Value, player.PlayerGui.Shop.Shop.ItemHolder.ItemShop.BigSword.Purchased.Value, player.PlayerGui.Shop.Shop.ItemHolder.ItemShop.BigSword.Equipped.Value}
			local itemdata = items

			for _, item in pairs(itemdata) do
				local success1, errormessage1 = pcall(function()
					ItemDataStore:SetAsync(playerUserId, item)
				end)
				if success1 then
					print("Saved Data")
				end

				if not success1 then
					print(errormessage1)
				end
			end
		end
	end)
	player.PlayerGui.Shop.Shop.ItemHolder.ItemShop.BigSword.Purchased.Changed:Connect(function()
		print("Change")
		for _, player in pairs(game.Players:GetChildren()) do
			local playerUserId = player.UserId
			local items = {player.PlayerGui.Shop.Shop.ItemHolder.ItemShop.Sword.Purchased.Value, player.PlayerGui.Shop.Shop.ItemHolder.ItemShop.Sword.Equipped.Value, player.PlayerGui.Shop.Shop.ItemHolder.ItemShop.BigSword.Purchased.Value, player.PlayerGui.Shop.Shop.ItemHolder.ItemShop.BigSword.Equipped.Value}
			local itemdata = items

			for _, item in pairs(itemdata) do
				local success1, errormessage1 = pcall(function()
					ItemDataStore:SetAsync(playerUserId, item)
				end)
				if success1 then
					print("Saved Data")
				end

				if not success1 then
					print(errormessage1)
				end
			end
		end
	end)
	player.PlayerGui.Shop.Shop.ItemHolder.ItemShop.BigSword.Equipped.Changed:Connect(function()
		print("Change")
		for _, player in pairs(game.Players:GetChildren()) do
			local playerUserId = player.UserId
			local items = {player.PlayerGui.Shop.Shop.ItemHolder.ItemShop.Sword.Purchased.Value, player.PlayerGui.Shop.Shop.ItemHolder.ItemShop.Sword.Equipped.Value, player.PlayerGui.Shop.Shop.ItemHolder.ItemShop.BigSword.Purchased.Value, player.PlayerGui.Shop.Shop.ItemHolder.ItemShop.BigSword.Equipped.Value}
			local itemdata = items

			for _, item in pairs(itemdata) do
				local success1, errormessage1 = pcall(function()
					ItemDataStore:SetAsync(playerUserId, item)
				end)
				if success1 then
					print("Saved Data")
				end

				if not success1 then
					print(errormessage1)
				end
			end
		end
	end)
	print("hi")
end)

while true do

	for _, player in pairs(game.Players:GetChildren()) do
		DataStore:SetAsync(player.UserId, player.leaderstats.Coins.Value)
	end

	for _, frame in pairs(game.Workspace.Leaderboard.LeaderboardGui.Holder:GetChildren()) do
		frame:Remove()
	end
	updateLeaderboard()
	print("Updated")
	wait(60)
end

Shop Script:

local Display = Shop.DisplayFrame
local DisplayItem = Display.DisplayItem
local DisplayButton = Display.DisplayButton
local CoinShop = ItemHolder.CoinsShop
local CosmeticShop = ItemHolder.CosmeticsShop
local ItemShop = ItemHolder.ItemShop
local GamepassShop = ItemHolder.GamepassShop
local player = game.Players.LocalPlayer
local MarketplaceService = game:GetService("MarketplaceService")
local items = {ItemShop.Sword.Purchased.Value, ItemShop.Sword.Equipped.Value, ItemShop.BigSword.Purchased.Value, ItemShop.BigSword.Equipped.Value}

game.Players.PlayerRemoving:Connect(function(player)

end)

coins.Value = 1000

-- SHOP BUTTONS
ShopButton.MouseButton1Up:Connect(function()
	if ShopOpen == false then
		Shop:TweenPosition(
			UDim2.new(0.292, 0, 0.078, 0),
			"Out",
			"Quad",
			1,
			false
		)
		for _, item in pairs(ItemShop:GetChildren()) do
			item.Visible = true
		end
		ShopOpen = true
	elseif ShopOpen == true then
		Shop:TweenPosition(
			UDim2.new(0.292, 0, -1, 0),
			"In",
			"Quad",
			1,
			false
		)
		wait(1)
		for _, item in pairs(CoinShop:GetChildren()) do
			item.Visible = false
		end
		for _, item in pairs(CosmeticShop:GetChildren()) do
			item.Visible = false
		end
		for _, item in pairs(ItemShop:GetChildren()) do
			item.Visible = false
		end
		for _, item in pairs(GamepassShop:GetChildren()) do
			item.Visible = false
		end
		for _, item in pairs(Display:GetChildren()) do
			item.Visible = false
		end
		ShopOpen = false
	end
end)

ItemButton.MouseButton1Up:Connect(function()
	for _, item in pairs(ItemShop:GetChildren()) do
		item.Visible = true
	end
	for _, item in pairs(CoinShop:GetChildren()) do
		item.Visible = false
	end
	for _, item in pairs(CosmeticShop:GetChildren()) do
		item.Visible = false
	end
	for _, item in pairs(GamepassShop:GetChildren()) do
		item.Visible = false
	end
end)
CosmeticButton.MouseButton1Up:Connect(function()
	for _, item in pairs(CosmeticShop:GetChildren()) do
		item.Visible = true
	end
	for _, item in pairs(CoinShop:GetChildren()) do
		item.Visible = false
	end
	for _, item in pairs(ItemShop:GetChildren()) do
		item.Visible = false
	end
	for _, item in pairs(GamepassShop:GetChildren()) do
		item.Visible = false
	end
end)
CoinsButton.MouseButton1Up:Connect(function()
	for _, item in pairs(CoinShop:GetChildren()) do
		item.Visible = true
	end
	for _, item in pairs(ItemShop:GetChildren()) do
		item.Visible = false
	end
	for _, item in pairs(CosmeticShop:GetChildren()) do
		item.Visible = false
	end
	for _, item in pairs(GamepassShop:GetChildren()) do
		item.Visible = false
	end
end)
GamepassButton.MouseButton1Up:Connect(function()
	for _, item in pairs(CoinShop:GetChildren()) do
		item.Visible = false
	end
	for _, item in pairs(ItemShop:GetChildren()) do
		item.Visible = false
	end
	for _, item in pairs(CosmeticShop:GetChildren()) do
		item.Visible = false
	end
	for _, item in pairs(GamepassShop:GetChildren()) do
		item.Visible = true
	end
end)
wait(1)
ItemShop.Sword.MouseButton1Up:Connect(function()
	if items[1] and items[2] then
		local inde = 3
	elseif items[1] and not items[2] then
		local inde = 2
	elseif not items[1] then
		local inde = 1
	end
	local inde = 1
	for _, item in pairs(Display:GetChildren()) do
		item.Visible = true
	end
	DisplayButton.MouseButton1Up:Connect(function()
		if inde == 3 then
			inde = 2
			items[2] = 0
			DisplayButton.Text = "Equip"
			ItemShop.Sword.Equipped.Value = 0
		elseif inde == 2 then
			inde = 3
			items[2] = 1
			DisplayButton.Text = "Unequip"
			ItemShop.Sword.Equipped.Value = 1
		elseif inde == 1 then
			if coins.Value >= 10 then
				inde = 2
				coins.Value = coins.Value - 10
				items[1] = 1
				DisplayButton.Text = "Equip"
				ItemShop.Sword.Purchased.Value = 1
			else
				print("")
			end
		end
	end)
	if items[1] and items[2] == 1 then
		DisplayItem.ImageLabel.Image = ItemShop.Sword.ImageLabel.Image
		DisplayButton.Text = "Unequip"
	elseif items[1] == 1 and items[2] == 0 then
		DisplayItem.ImageLabel.Image = ItemShop.Sword.ImageLabel.Image
		DisplayButton.Text = "Equip"
	elseif items[1] == 0 then
		DisplayItem.ImageLabel.Image = ItemShop.Sword.ImageLabel.Image
		DisplayButton.Text = "Purchase"
	end
end)

-- Set Coins
while true do
	wait(1)
	items = {ItemShop.Sword.Purchased, ItemShop.Sword.Equipped, ItemShop.BigSword.Purchased, ItemShop.BigSword.Equipped}
	print(items)
	CoinHolder.Text = tostring(coins.Value)
end

Any help is appreciated :slight_smile:

1 Like

Could somebody please help me with this?

So If you have the shop set up look for tutorials on how to make a leaderstats save if that doesn’t work then tell me and I will write a script to save leaderstats for you.

Also if your shop is tools search up online for a toolsaving script if not I will figure something out to.

I already have a leaderstats saving script (it saves the coins, it doesn’t work with these scripts because I have it so when you join you have 1000 coins for testing shop). But I will try the tool saving.

I mean the saving leaderstats script will keep all coins you ever have even if you change the 1000 I think it will add coins to what ever you change it. Thats just the system.