Data problems on an GUI shop

Hello Developers!
Recently I have tried to make a leaderstat system where players get money each 5 seconds and it saves if they leave as well as a GUI shop connected to it.

My problem is that when I purchase on the GUI my money gets substracted but then it goes back like before, according to me it may be a datastore issue, how do i fix it?

There are 2 scripts the first one is for the data and the system on ServerScriptService
I will list them here as well as a video of the issue.

ServerScript service:

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local MoneyDataStore = DataStoreService:GetDataStore("PlayerMoney")

local function savePlayerData(player)
	local success, error = pcall(function()
		MoneyDataStore:SetAsync(player.UserId .. "_Money", player.leaderstats.Money.Value)
	end)
	if not success then
		warn("Error saving data for player " .. player.Name .. ": " .. error)
	end
end

local function loadPlayerData(player)
	local success, data = pcall(function()
		return MoneyDataStore:GetAsync(player.UserId .. "_Money")
	end)
	if success and data then
		player.leaderstats.Money.Value = data
	end
end

local function giveMoney()
	for _, player in ipairs(Players:GetPlayers()) do
		if player and player.Parent then
			player.leaderstats.Money.Value = player.leaderstats.Money.Value + 10 -- Adjust the amount of money earned here
		end
	end
end

local function playerAdded(player)
	-- Create leaderstats if they don't exist
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	-- Create money stat
	local money = Instance.new("IntValue")
	money.Name = "Money"
	money.Value = 0 -- Set initial money value here
	money.Parent = leaderstats

	-- Load player's money from DataStore
	loadPlayerData(player)
end

local function playerRemoving(player)
	savePlayerData(player)
end

-- Connect events
game.Players.PlayerAdded:Connect(playerAdded)
game.Players.PlayerRemoving:Connect(playerRemoving)

-- Give money every 5 seconds
while true do
	giveMoney()
	wait(5)
end```

Now the Local Script on the button of the shop.


-- Function to handle the purchase button click
local function purchaseButtonClicked()
	local player = game.Players.LocalPlayer

	-- Get the player's leaderstats
	local leaderstats = player:WaitForChild("leaderstats")

	-- Get the player's money value
	local moneyValue = leaderstats:WaitForChild("Money")

	-- Check if the player has enough money to make the purchase
	if moneyValue.Value >= 10 then -- Adjust the price of the item here
		-- Subtract the item price from the player's money
		moneyValue.Value = moneyValue.Value - 10 -- Adjust the price of the item here

		-- Give the item to the player
		local item = game.ReplicatedStorage.Items.Periastron:Clone() -- Replace "ItemName" with the name of your item
		item.Parent = player.Backpack
	else
		-- Player doesn't have enough money, display an error message or take appropriate action
		print("Not enough money to purchase the item!")
	end
end

-- Assuming you have a TextButton named "PurchaseButton"
local purchaseButton = script.Parent -- Reference to the button itself

-- Connect the button click event to the purchaseButtonClicked function
purchaseButton.Activated:Connect(purchaseButtonClicked)

You need a RemoteEvent(or RemoteFunction) that fires to the server every time the player presses the buy button. You then check on the server if they can afford the item that they are trying to buy. If so, then subtract money from them and give them the item. If not, do nothing. Checking the value of currency on the client doesn’t do anything on the server and is easily exploitable. The same thing goes for giving the player an item on the client.

You’re changing the value on the client. (like what @bleintant said, use a remote event and a server script to check if the player has the money or whatever)

This is an absolutely error of the Client and Server, this is not a datastorage issue, it’s because when you take the money on local script, it just changes for you, but for the game you still having the same money, the solution is to add a remote event and check on a server script if the player has the money and then give the reward.

ok thank you ill try to do that

ok thank you i will try to do that and see if it works

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.