Data store not saving value from purchase

Hi! I have this shop but I have a problem: The data store is not saving the shop purchases. Here is my shop gui script:

local Plr = game.Players.LocalPlayer

local button = script.Parent.EquipButton
local button2 = script.Parent.UnequipButton
local button3 = script.Parent["Buy - 50 Coins"]

button2.Visible = false
button.Visible = false
button3.Visible = true

button3.MouseButton1Click:Connect(function()
	if Plr.leaderstats.Coins.Value >= 50 then
		Plr.leaderstats.Coins.Value -= 50
	else
		button3.Text = "Not enough coins!"
		wait(2)
		button3.Text = "Buy - 50 Coins"
	end
end)

button.MouseButton1Click:Connect(function()
	if button.Visible == true then 
		game.ReplicatedStorage.BloxyCola:Clone().Parent = Plr:WaitForChild("Backpack")
		button.Visible = false
		button2.Visible = true
	end

end)

button2.MouseButton1Click:Connect(function()
	if button.Visible == false and button2.Visible == true then
		button.Visible = true
		button2.Visible = false
		local burger = Plr:WaitForChild("Backpack"):WaitForChild("BloxyCola")
		burger:Destroy()
	end
end)

And the data store script:

-- // Assigning variables //
local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("MyDataStore") -- This can be changed to whatever you want

local chatEvent = game.ReplicatedStorage:WaitForChild("Achievement")

local badge = game:GetService("BadgeService")

local function saveData(player) -- The functions that saves data

	local tableToSave = {
		player.leaderstats.Coins.Value;
		player.leaderstats.Wins.Value;
		player.Visits.Value
	}

	local success, err = pcall(function()
		dataStore:SetAsync(player.UserId, tableToSave) -- Save the data with the player UserId, and the table we wanna save
	end)

	if success then -- If the data has been saved
		print("Data has been saved!")
	else -- Else if the save failed
		print("Data has not been saved!")
		warn(err)		
	end
end

game.Players.PlayerAdded:Connect(function(player) -- When a player joins the game
	
	-- // Assigning player stats //
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Coins = Instance.new("IntValue")
	Coins.Name = "Coins"
	Coins.Parent = leaderstats
	Coins.Value = 0
	
	local Wins = Instance.new("IntValue")
	Wins.Name = "Wins"
	Wins.Parent = leaderstats
	Wins.Value = 0
	
	local Visits = Instance.new("IntValue")
	Visits.Name = "Visits"
	Visits.Parent = player
	Visits.Value = 1
	
	Wins.Changed:Connect(function()
		
		if player.leaderstats.Wins.Value == 1 then

			local success, errmsg = pcall(function()
				badge:AwardBadge(player.UserId, 2127079658)
			end)

			if success then
				print("Awarded!")
			else
				warn("Failed to award badge!")
				warn(errmsg)
			end
		end

		if player.leaderstats.Wins.Value == 2 then

			local success, errmsg = pcall(function()
				badge:AwardBadge(player.UserId, 2127079679)
			end)

			if success then
				print("Awarded!")
			else
				warn("Failed to award badge!")
				warn(errmsg)
			end
		end
		
	end)
	
	Visits.Changed:Connect(function()
		
		if Visits.Value == 10 then
			local success, errmsg = pcall(function()
				badge:AwardBadge(player.UserId, 2127209356)
			end)

			if success then
				print("Awarded!")
			else
				warn("Failed to award badge!")
				warn(errmsg)
			end
		end
		
		if Visits.Value == 25 then
			local success, errmsg = pcall(function()
				badge:AwardBadge(player.UserId, 2127209520)
			end)

			if success then
				print("Awarded!")
			else
				warn("Failed to award badge!")
				warn(errmsg)
			end
		end
		
		if Visits.Value == 50 then
			local success, errmsg = pcall(function()
				badge:AwardBadge(player.UserId, 2127209540)
			end)

			if success then
				print("Awarded!")
			else
				warn("Failed to award badge!")
				warn(errmsg)
			end
		end
		
	end)

	local data -- We will define the data here so we can use it later, this data is the table we saved
	local success, err = pcall(function()
		data = dataStore:GetAsync(player.UserId) -- Get the data from the datastore
	end)

	if success and data then -- If there were no errors and player loaded the data

		Coins.Value = data[1] -- Set the coins to the first value of the table (data)
		Wins.Value = data[2] -- Set the wins to the second value of the table (data)
		Visits.Value = data[3]
		Visits.Value += 1
		
	else -- The player didn't load in the data, and probably is a new player
		print("The player has no data!") -- The default will be set to 0
	end

end)

game.Players.PlayerRemoving:Connect(function(player) -- When a player leaves
	local success, err  = pcall(function()
		saveData(player) -- Save the data
	end)

	if success then
		print("Data has been saved!")
	else
		print("Data has not been saved!")
		warn(err)
	end
end)

game:BindToClose(function() -- When the server shuts down
	for _, player in pairs(game.Players:GetPlayers()) do -- Loop through all the players
		local success, err  = pcall(function()
			saveData(player) -- Save the data
		end)

		if success then
			print("Data has been saved!")
		else
			print("Data has not been saved!")
			warn(err)
		end
	end
end)

The Visits and Wins are not important, it’s the Coins I’m worried about.

Video of test:
robloxapp-20220709-1008251_Trim.wmv (806.9 KB)

But when I rejoin, it resets to the previous value that I had before I bought the item. Please help me fix this issue.

1 Like

It’s because your decreasing the values locally, not server-side which is why it doesn’t detect the changes. Learn about remote events and use one for your shop.

2 Likes

Oh ok. I’ll try what you did. (Char limit)

Works perfectly:

Server Script:

game.ReplicatedStorage:WaitForChild("Purchased").OnServerEvent:Connect(function(player, cost)
	player.leaderstats.Coins.Value -= cost
end)

New local script:

local Plr = game.Players.LocalPlayer

local button = script.Parent.EquipButton
local button2 = script.Parent.UnequipButton
local button3 = script.Parent["Buy - 50 Coins"]

button2.Visible = false
button.Visible = false
button3.Visible = true

local purchase = game.ReplicatedStorage:WaitForChild("Purchased")

button3.MouseButton1Click:Connect(function()
	if Plr.leaderstats.Coins.Value >= 50 then
		purchase:FireServer(50)
	else
		button3.Text = "Not enough coins!"
		wait(2)
		button3.Text = "Buy - 50 Coins"
	end
end)

button.MouseButton1Click:Connect(function()
	if button.Visible == true then 
		game.ReplicatedStorage.BloxyCola:Clone().Parent = Plr:WaitForChild("Backpack")
		button.Visible = false
		button2.Visible = true
	end

end)

button2.MouseButton1Click:Connect(function()
	if button.Visible == false and button2.Visible == true then
		button.Visible = true
		button2.Visible = false
		local burger = Plr:WaitForChild("Backpack"):WaitForChild("BloxyCola")
		burger:Destroy()
	end
end)

Thanks so much for your help.

3 Likes

No problem, if you have any more questions, feel free to ask.

2 Likes