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)
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.
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)