Cash refunded after player mines block

(Mining game)
Whenever you have a certain amount of cash, you press the “Level Up” button and it increases your Level by 1 while deducing the cash. The problem is, whenever you purchase that “Level Up”, it takes away the cash but then whenever you mine your next block, it refunds all of that cash + the cash you got from that current block.

I tried looking for other threads and topics related to this issue but couldn’t find an exact solution.

plr = game.Players.LocalPlayer
cash = plr.leaderstats.Cash
level = plr.leaderstats.Level

script.Parent.MouseButton1Click:Connect(function()
if cash.Value >= 100 then
plr.leaderstats.Cash.Value = plr.leaderstats.Cash.Value - 100
plr.leaderstats.Level.Value = plr.leaderstats.Level.Value + 1

else
print(“You do not have enough cash”)

end
end)

wait(10)

Let me know if you need any other scripts or explainations to make things more clear.

The problem here is that you are trying to change the player’s leaderstats through a localscript (on the client). Try instead using remote events or remote functions to send a request over to the server to adjust the player’s leaderstats. What you change on the client will not replicate to the server with some exceptions. Custom Events and Callbacks | Documentation - Roblox Creator Hub

A solution could be to: when the button is pressed, send a remote function that checks their cash value from inside the server script as opposed to inside this local script.

Here is something I quickly whipped up, please excuse any flaws/errors, as it is just to show the general idea.

local script example:

local remotefunction = whereeveryourremotefunctionis

script.Parent.MouseButton1Click:Connect(function()
    if remotefunction:InvokeServer() then
       -- remote returned true!  success
    else
      print(“You do not have enough cash”)
       -- remote returned false!
    end
end)

server script example:

local remotefunction = whereeveryourremotefunctionis

remotefunction.OnServerInvoke = function(plr)
if plr:FindFirstChild("leaderstats") and plr.leaderstats:FindFirstChild("Cash") and plr.leaderstats:FindFirstChild("Level") then

local cash = plr.leaderstats.Cash
local level = plr.leaderstats.Level

    if cash.Value >= 100 then
      plr.leaderstats.Cash.Value = plr.leaderstats.Cash.Value - 100
      plr.leaderstats.Level.Value = plr.leaderstats.Level.Value + 1
      return true
    else 
      return false
    end

end

end)
1 Like

Thank you! It was a big help. I was also using remoteEvent instead of a remoteFunction so that part of your script helped me greatly.

1 Like

I just wanted to step in and say that your note about client to server replication is incorrect since there are things that will replicate instantly if the client changes them, if you want to know about them then go to this article: Roblox Client-Server Model

You need to specifically look at what’s at the end of the article, here’s an image of it:

As you can see you are incorrect, so next time please do not tell people misinformation in the future because that will severely hurt their development career and they will have an extremely bad picture in their head of how that thing works.