Buying System working but the player reverts to their original currency right after

I am currently making a shop system to buy weapons which works although when the player buys something the player reverts back to their original money a second later.

The currency is time, which counts down every second until the player would die. So when the player’s money reverts to what it was the value is a second less then before the purchase which makes me think that the script for the time to count down doesn’t detect when the time is subtracted from something else.

Here is the script which counts down the time:

while true do
	wait(1)
	if player.Team == game.Teams["Alive"] then
		Time.Value = Time.Value - 1
	end
	if Time.Value <= 0 then
		local Character = player.Character or player.CharacterAdded:Wait()
		Character.Humanoid.Health = 0
	end
end

These lines of code are inside of the leaderstats script

Here is the buying script inside of the gui buy button:

local tool = script.Parent.Tool.Value

script.Parent.MouseButton1Click:Connect(function(player)
	if game.Players.LocalPlayer.leaderstats.Time.Value >= script.Parent.Cost.Value then
		game.Players.LocalPlayer.leaderstats.Time.Value = game.Players.LocalPlayer.leaderstats.Time.Value - script.Parent.Cost.Value
		game.ReplicatedStorage.BuyTool:FireServer(tool, player)
		script.Parent.Text = "Success!"
		wait(1)
		script.Parent.Text = script.Parent.Cost.Value.." Seconds"
	else
		script.Parent.Text = "You Need More Seconds!"
		wait(1)
		script.Parent.Text = script.Parent.Cost.Value.." Seconds"
	end
end)

Any help is heavily appreciated.

local tool = script.Parent.Tool.Value

script.Parent.MouseButton1Click:Connect(function(player)
	if game.Players.LocalPlayer.leaderstats.Time.Value >= script.Parent.Cost.Value then
		game.Players.LocalPlayer.leaderstats.Time.Value = game.Players.LocalPlayer.leaderstats.Time.Value - script.Parent.Cost.Value
		game.ReplicatedStorage.BuyTool:FireServer(tool, player)
		script.Parent.Text = "Success!"
	else
		script.Parent.Text = "You Need More Seconds!"
	end
	wait(1)
	script.Parent.Text = script.Parent.Cost.Value.." Seconds"
end) ```

With this script the players money should be updated correctly and the time should count down properly after the purchase.

I am happy that you tried to help me, although this script acts the exact same as the original only you moved where it changes the text back to normal

1 Like

use a RemoteFunction to handle the purchase request from the client.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local BuyToolRemote = ReplicatedStorage:WaitForChild("BuyToolRemote")

-- Update the player's currency when a purchase is made
script.Parent.MouseButton1Click:Connect(function(player)
    local cost = script.Parent.Cost.Value
    local playerCurrency = player.leaderstats.Time.Value
    if playerCurrency >= cost then
        player.leaderstats.Time.Value = playerCurrency - cost
        local success, result = pcall(function()
            return BuyToolRemote:InvokeServer(tool)
        end)
        if not success then
            error("Purchase failed: " .. result)
        end
        script.Parent.Text = "Success!"
        wait(1)
        script.Parent.Text = cost .. " Seconds"
    else
        script.Parent.Text = "You Need More Seconds!"
        wait(1)
        script.Parent.Text = cost .. " Seconds"
    end
end)

yeah, this one had the same results (a couple other errors too but those I fixed)

although I might try just making the money removal happen on the server side bc I had a bug similar to this and that was the solution.