DataStore not saving properly

Hi again, I have another problem with my DataStore. It doesn’t save my data anymore, it stopped doing that since I made the script in this post. The script in there is not the fixed one, although it didn’t work at the time either. When I purchase a tool, it deducts the cash from my DataSave and gives me the tool, although when I leave and rejoin it gave me back my Points and I don’t have that item anymore. Another problem with this is that when I touch a part and own the item (that part is invisible and gives 5 points upon touch), instead of only giving me 5 Points it gives me 50 Points (the item cost) + the 5 points, and I get to keep the item… how should I fix this? here is the DataSaving script:

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

game.Players.PlayerAdded:Connect(function(player)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Points = Instance.new("IntValue")
	Points.Name = "Points"
	Points.Parent = leaderstats
	
	local Wins = Instance.new("IntValue")
	Wins.Name = "Wins"
	Wins.Parent = leaderstats
	
	local Activations = Instance.new("IntValue")
	Activations.Name = "Activations"
	Activations.Parent = leaderstats
	
	local Deaths = Instance.new("IntValue")
	Deaths.Name = "Deaths"
	Deaths.Parent = leaderstats
	
	-->> Items: Items owned and unowned
	
	local ItemsOwned = Instance.new("Folder")
	ItemsOwned.Name = "ItemsOwned"
	ItemsOwned.Parent = player
	
	local LoveSignOwned = Instance.new("BoolValue")
	LoveSignOwned.Name = "LoveSignOwned"
	LoveSignOwned.Parent = ItemsOwned
	
	-->> Data: Begining
	
	local playerUserId = "Player_"..player.UserId
	
	-->>Data: Load Data
	
	local data
	local success, errormessage	 = pcall(function()
		data = myDataStore:GetAsync(playerUserId)
	end)
	
	if success then
		if data then
			Points.Value = data.Points
			Wins.Value = data.Wins
			Activations.Value = data.Activations
			Deaths.Value = data.Deaths
			--Items Owned
			LoveSignOwned.Value = data.LoveSignOwned
		end
	end
	
end)

-->> Data: Saving Stats

game.Players.PlayerRemoving:Connect(function(player)
	local playerUserId = "Player_"..player.UserId
	
	local data = {
		Points = player.leaderstats.Points.Value;
		Wins = player.leaderstats.Wins.Value;
		Activations = player.leaderstats.Activations.Value;
		Deaths = player.leaderstats.Deaths.Value;
		-- Items Owned
		LoveSignOwned = player.ItemsOwned.LoveSignOwned.Value;
	}
	
	local success, errormessage = pcall(function()
		myDataStore:SetAsync(playerUserId, data)
	end)
		
	if success then
		print("Data succefully saved!")
	else
		print("Data saving error")
		warn(errormessage)
	end
	
end)

Here is the tool giving/buying script:

local Players = game:GetService("Players")
local plr = Players.LocalPlayer
local replicatedStorage = game:GetService("ReplicatedStorage");
local player = game:GetService("Players").LocalPlayer;
local backpack = player:WaitForChild("Backpack");

local tool1 = "LoveSign";
local toolClone;
local button = script.Parent
local frame = script.Parent.Parent

local ItemName = frame.ItemName
local ItemDescription = frame.ItemDescription
local ItemPurchaseButton = frame["Purchase/EquipItem"]
local ItemImage = frame.ItemImageFrame.ImageLabel


script.Parent.MouseButton1Click:Connect(function()
	if ItemName.Text == "Love sign" and plr.ItemsOwned.LoveSignOwned.Value == false and plr.leaderstats.Points.Value >= 50 then
		plr.ItemsOwned.LoveSignOwned.Value = true
		ItemPurchaseButton.Text = "Equip"
		plr.leaderstats.Points.Value = plr.leaderstats.Points.Value - 50
	elseif ItemName.Text == "Love sign" and plr.ItemsOwned.LoveSignOwned.Value == true and ItemPurchaseButton.Text == "Equip" then
		if not toolClone then
			toolClone = replicatedStorage:FindFirstChild("LoveSign"):Clone()
			toolClone.Parent = backpack
			button.Text = "Unequip"
		end
	elseif ItemName.Text == "Love sign" and ItemPurchaseButton.Text == "Unequip" then
		toolClone.Parent = nil;
		toolClone = nil;
		button.Text = "Equip"
	end
end)
1 Like

Sometime when a player leaves, they leave before the server manages to save their data. Therefore it is good to save stuff regularly, even best once something occurs. As an example, the purchase of an item.

Alright, fair point, but I left the game like a solid 25 times and it NEVER saved the data. By the way how do I make DataSaving loop so that its not ONLY when someone leaves?

So how could I fix this please?.. This bug is breaking my game…

This is because you are changing the data on the client, hence the local script, and anything done on the client does not replicate to the sever. I would recommend you using remote events:
In a local script, do

local remote = game:GetService(“ReplicatedService”):WaitForChild(“RemoteEvent”)
remote:FireServer(numberofpointstotake)

Then in a server script you would do:

local remote = game:GetService(“ReplicatedService”):WaitForChild(“RemoteEvent”)
remote.OnServerEvent:Connect(function(player, subtract)
    leaderstats.points.Value = leaderstats.points.Value x subtractpoints
end)

Alright, thanks you! i will try this and come back to it.