Player's leaderstats dont decrease

  1. What do you want to achieve? Keep it simple and clear!
    I want to decrease the player’s leaderstats cash value.
  2. What is the issue? Include screenshots / videos if possible!
    The value doesnt decrease
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I looked for solutions in the developer hub but i found nothing.

So basically I want to make it so when a player presses a button then he looses some money and he gets a tool. When i press the button I get the tool but I dont loose any money (i used number values for this). This is the script.

local player = script.Parent.Parent.Parent.Parent.Parent.Parent.Parent
local money = player.leaderstats.Cash 
local price = script.Parent.Price.Value
local tool = game.ServerStorage.M4A1
local priceparent = script.Parent.Price

function buy()
	if money.Value >= price then
		money.Value = money.Value - price
		local a = tool:clone()
		a.Parent = player.Backpack
	else
		if money.Value <= price then
		script.Parent.PurchaseMoneyBackground.Visible = true
		end
		end
end

script.Parent.Accept.MouseButton1Click:connect(buy)

Any help is appreciated! :smiley:

2 Likes

The biggest question is are you using a server script for the UI?
I would like to assume so because the biggest issue I’m immediately seeing is that you are doing this for UI and you are referencing ServerStorage.

Since ServerStorage cannot be accessed by the client, I’d like to assume you’re using a server script.

1 Like

Yes, I am using a server script.

I would recommend using the textbutton to fire an event instead of having a serverscript in the UI, as that is not really viable.
Create a RemoteEvent in ReplicatedStorage named “buy”
Create a LocalScript as a Child to the textbutton you are using to buy things

local a = game.ReplicatedStorage.buy
local price = -- insert the price here
local tool = "M4A1"
script.Parent.MouseButton1Down:Connect(function()
a:FireServer(price,tool)
end)

and then create a serverscript in serverscriptservice, with the following code

local a = game.ReplicatedStorage.buy
a.OnServerEvent:Connect(function(player,price,tool)
       player:WaitForChild("leaderstats").Cash.Value = player:WaitForChild("leaderstats").Cash.Value - tonumber(price)
if tool == "M4A1" then
      local b = game.ServerStorage.M4A1:Clone()
      b.Parent = player.Backpack
end
end)

Still the lederstats value does not get decreased.

could you show (if applicable) what error it shows? or like the leaderstats script

Okay, that isn’t the best idea and is a problem. Especially if you are allowing multiple players in a server, it is going to run that code for every single client.

Here is new code I wrote out. Delete the old script, add a LocalScript in its place (with the same name of course,) and paste this code into it:

local Players = game:GetService("Players")
local replicatedStorage = game:GetService("ReplicatedStorage")

local player = Players.LocalPlayer
local money = player:WaitForChild("leaderstats"):WaitForChild("Cash")
local price = script.Parent:WaitForChild("Price")
local priceValue = price.Value

script.Parent:WaitForChild("Accept").MouseButton1Click:Connect(function()
	replicatedStorage:WaitForChild("GivePlayerTool"):FireServer(priceValue, "M4A1")
end)

You’ll also need to add a server script into ServerScriptService, name it what ever you’d like, and paste this code into it.

local replicatedStorage = game:GetService("ReplicatedStorage")
local serverStorage = game:GetService("ServerStorage")

replicatedStorage:WaitForChild("GivePlayerTool").OnServerEvent:Connect(function(player, price, tool)
	if player.leaderstats.Cash.Value >= price and not player.Backpack:FindFirstChild(tool) then
		player.leaderstats.Cash.Value -= price
		
		local toolClone = serverStorage:FindFirstChild(tool):Clone()
		toolClone.Parent = player.Backpack
	else
		warn("Error: Player does not have enough cash or it is already in their inventory!")
	end
end)

After that, add a RemoteEvent into ReplicatedStorage with the exact name “GivePlayerTool.”

After that, it should hopefully work. Try it out, and if it doesn’t, let me know!

1 Like

server scripts cannot detect input of GUI

I made a small testing game and sort of jerry-rigged it together using the code I wrote. Like, I added the cash leaderstat, UI and all that stuff to make sure.

I can confirm it gives you both the tool and subtracts the amount of money. Though, you’ll have to piece it together and make sure it works for you. I don’t see why it wouldn’t work for you since it does for me, but if it doesn’t please let me know!

I had to use my code in a different way for this to work. It took me longer time that it should have but I figured it out.

It was a bit difficult for me to understand what you said, I’m sorry about that. Been a bit confused today (I’m sick and it’s slowing my brain down a bit lol.)

So, my main question is were you saying that you got it to work?

Try changing

script.Parent.Accept.MouseButton1Click:connect(buy)

to

script.Parent.Accept.MouseButton1Click:Connect(buy)
Working test version
local player = script.Parent.Parent.Parent.Parent
local money = player:WaitForChild("leaderstats"):WaitForChild("Cash")
local price = 1
local tool = game.ServerStorage.M4A1

function buy()
	if money.Value >= price then
		money.Value = money.Value - price
		local a = tool:clone()
		a.Parent = player.Backpack
	else
		print('not enough money')
	end
end

script.Parent.MouseButton1Click:Connect(buy)

On top of that, here’s my game and explorer when I click the buy button:


My default cash is set to 100, so it did all that was intended. Subtracted my price which is set to 1 for testing purposes and gave me the tool.

Yeah but using a different way.

1 Like

The point is that i wanted the price to be a number value

Alrighty then. I apologize for my confusion and if I wasted any time.

Ah sorry I misunderstood glad you got it working though