I need help with my shop DataStore

hello! Im currently trying to have a shop save using datastores, but it doesnt use items. what it does is that the player starts with a wand doing 1 spell every clicks, like in every other simulators but when you buy the next upgrade, instead of giving the player a new item, it changes the intvalue of the wand making it do more spells per clicks.

I already have datastore to save coins and spells so I tried adding the IntValue to the script but it only made it so when the player clicked it didnt give any spells.

here’s the shop script if you’re struggling to understand the shop concept:

local Player = game.Players.LocalPlayer
local leaderstats = Player:WaitForChild("leaderstats")
local HiddenStats = Player:WaitForChild("HiddenStats")
local SpellGivenAfterPurchase = script.Parent.Parent.SpellGivenAfterPurchase
local RS = game:GetService("ReplicatedStorage")
local Remotes = RS:WaitForChild("Remotes")
local Price = script.Parent.Parent.Price

local StarterGui = game:GetService("StarterGui")

local function SendNotification(text)
	StarterGui:SetCore("SendNotification", {
		Title = "Notification",
		Text = text,
		Duration = 3
	})
end

script.Parent.MouseButton1Click:Connect(function()
	
	local result = Remotes.ReturnValueForShop:InvokeServer("HiddenStats", "CurrentSpell")
	print(result, SpellGivenAfterPurchase.Value)
	if result < SpellGivenAfterPurchase.Value then
		
		local currentCoins = Remotes.ReturnValueForShop:InvokeServer("leaderstats", "coins")
		print(currentCoins, Price.Value)
		if currentCoins >= Price.Value then
			
			Remotes.AddCurrentSpell:FireServer(SpellGivenAfterPurchase.Value, Price.Value)
			script.Parent.Text = "bought"
			
		else
			-- warn("You do not have enough coins!")
			SendNotification("You do not have enough coins!")
		end
	else
		-- warn("You can only buy bigger spells!")
		SendNotification("You can only buy bigger spells!")
	end
	
end)

and here’s the current datastore scripts in server script service (without what I tried I removed it cuz it messed with the spells)

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")

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

	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = 'leaderstats'
	
	local HiddenStats = Instance.new("Folder")
	HiddenStats.Name = "HiddenStats"
	HiddenStats.Parent = player
	
	local CurrentSpell = Instance.new("IntValue")
	CurrentSpell.Name = "CurrentSpell"
	CurrentSpell.Value = 1
	CurrentSpell.Parent = HiddenStats
	
	local spells = Instance.new("IntValue", leaderstats)
	spells.Name = 'spells'
	spells.Value = 0
	
	local coins = Instance.new("IntValue",leaderstats)
	coins.Name = 'coins'
	coins.Parent = leaderstats
	coins.Value = 0
	

		local playerUserId = "Player"..player.UserId

		local data = {
			coins = player.leaderstats.coins.Value,
		spells = player.leaderstats.spells.Value
		}
		local success, errormessage = pcall(function()
			data = myDataStore:GetAsync(playerUserId)
		end)

		if success then
			if data then
				coins.Value = data.coins
			spells.Value = data.spells

			end
		else
			warn(errormessage)
		end
	end)


	game.Players.PlayerRemoving:Connect(function(player)
		local playerUserId = "Player"..player.UserId
		local data = {
			coins = player.leaderstats.coins.Value,
		spells = player.leaderstats.spells.Value
		}

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

		if success then
			print("data saved")
		else
			print("error when saving data")
			warn(errormessage)
		end
	end)

sorry, its quite long. I just wanna make sure people understands the problem.

1 Like

You are implying that data already exists before using GetAsync

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")

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

	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = 'leaderstats'
	
	local HiddenStats = Instance.new("Folder")
	HiddenStats.Name = "HiddenStats"
	HiddenStats.Parent = player
	
	local CurrentSpell = Instance.new("IntValue")
	CurrentSpell.Name = "CurrentSpell"
	CurrentSpell.Value = 1
	CurrentSpell.Parent = HiddenStats
	
	local spells = Instance.new("IntValue", leaderstats)
	spells.Name = 'spells'
	spells.Value = 0
	
	local coins = Instance.new("IntValue",leaderstats)
	coins.Name = 'coins'
	coins.Parent = leaderstats
	coins.Value = 0
	

		local playerUserId = "Player"..player.UserId

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

		if success then
			if data ~= nil then
				player.leaderstats.coins.Value = data.coins
			player.leaderstats.spells.Value = data.spells

			end
		else
			warn(errormessage)
		end
	end)


	game.Players.PlayerRemoving:Connect(function(player)
		local playerUserId = "Player"..player.UserId
		local data = {
			coins = player.leaderstats.coins.Value,
		spells = player.leaderstats.spells.Value
		}

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

		if success then
			print("data saved")
		else
			print("error when saving data")
			warn(errormessage)
		end
	end)

so I tried it with what I tried before to add the intvalue data and when the player clicked it stopped working again. here’s what I did

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")

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

	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = 'leaderstats'
	
	local HiddenStats = Instance.new("Folder")
	HiddenStats.Name = "HiddenStats"
	HiddenStats.Parent = player
	
	local CurrentSpell = Instance.new("IntValue")
	CurrentSpell.Name = "CurrentSpell"
	CurrentSpell.Value = 1
	CurrentSpell.Parent = HiddenStats
	
	local spells = Instance.new("IntValue", leaderstats)
	spells.Name = 'spells'
	spells.Value = 0
	
	local coins = Instance.new("IntValue",leaderstats)
	coins.Name = 'coins'
	coins.Parent = leaderstats
	coins.Value = 0
	

		local playerUserId = "Player"..player.UserId

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

		if success then
		if data ~= nil then
				coins.Value = data.coins
			spells.Value = data.spells
			CurrentSpell.Value = data.CurrentSpell

			end
		else
			warn(errormessage)
		end
	end)


	game.Players.PlayerRemoving:Connect(function(player)
		local playerUserId = "Player"..player.UserId
		local data = {
			coins = player.leaderstats.coins.Value,
		spells = player.leaderstats.spells.Value,
		CurrentSpell = player.HiddenStats.CurrentSpell.Value
		}

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

		if success then
			print("data saved")
		else
			print("error when saving data")
			warn(errormessage)
		end
	end)