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)