Change Player's Leaderstats Value On Button Click

Hello everyone. It’s been a WHILE since I’ve developed on Roblox, and a lot has probably changed. I’m trying to subtract money from a player’s leaderstat when a SurfaceGui’s button is pressed.

Right now, I have a LocalScript that attempts to fetch the Player and their Cash value.

local gamePrice = script.Parent:WaitForChild("Price").Value -- 500

local button = script.Parent -- a text button

button.MouseButton1Click:Connect(function()
	local Players = game:GetService("Players")
	local player = Players.LocalPlayer
	local money = player:WaitForChild("leaderstats"):WaitForChild("Cash") -- this all looks right, as Cash is the name for the leaderstat thingy or whatever...you get what i mean
	
	money.Value = money.Value - gamePrice -- attempting to subtract the price of the game (500) by the player's original leaderstat
	
	-- rest of code goes here
end)

*I tried to add comments where helpful.

BUT… this doesn’t work, and I’ve been trying to figure this out since last night. Below is what my hierarchy looks like. If it helps, the value of Price is 500.

image

Unfortunately, my script doesn’t work, and I’m wondering if I need to fire a Remote Event or something else so that the leaderstats update. I’ve never worked with Remote Events before, and even with the documentation, it’s all confusing to me. If anyone could explain it to me and give an EXAMPLE of code, that would be greatly appreciated.

By the way, no errors arise after pressing the button. Even when I insert a print() function to test if the button’s press was detected, it does not appear.
image

Any help would be greatly appreciated.

1 Like

I am not sure, i think the Script won’t even run. Just print at the very top print(“Is Running”). If its not, then its because LocalScripts don’t run in workspace (if its in workspace)

It’s not in Workspace, the TextButton is the parent of the LocalScript. However, per your request, I followed this, and it is in fact not running. I am getting zero output from the LocalScript whatsoever. Any way to combat this?

Where is the whole Model/Instance located in?

Workspace. I think I just contradicted myself :skull:

SurfaceGui must be under starterGui and use the Adornee and set it to your part in the workspace.

But I can help you of solving the issue move everything (except the part) and the surfaceGUI into StarterGUI.In the SurfaceGUI, there is a property called “Adornee” set that to the part of where you want the button to be located in. Then just align with the property “Face” and then the local script should work.

I think that you would have to do this in a server script. Put this in your localscript instead:

script.Parent.MouseButton1Click:Connect(function()
	game.ReplicatedStorage.RemoteEventHere:FireServer(gamePrice) --I would put the gameprice variable inside of a serverscript so that exploiters can't change it
end)

Server script:

game.ReplicatedStorage.RemoteEventHere.OnServerEvent:Connect(function(player, gamePrice)
	player.leaderstats:WaitForChild("Cash").Value = plr.leaderstats:WaitForChild("Cash").Value - gamePrice
end)

I probably messed up somewhere in the script but hopefully this helps. Remember to add a remote event into ReplicatedStorage and change its name.

Edit: i forgot that you don’t need to find the player in the server script lol
Edit 2: I just realized that this is a SurfaceGui, not a ScreenGui. Please forget that I ever made this post.

1 Like

Innefficient + it can be done easier. As quakage and me said.

1 Like

mouse button click should only be used to run a remote event to the server, as what polysaccharide did. Everything else should be run in server to change leaderboard stats and stop exploiters

Wow. Worked like a charm. Thank you (and @yoshicoolTV) for giving me this solution. @PolyLacticSugarcane, I tried your script at first as I was skeptical of this answer, but I do agree on the exploit part. I will make changes to that. Thank you all for your speedy responses :heart:

1 Like

Where would I put the server script? ServerScriptStorage?

ServerScriptService or under the gui itself

1 Like

Beautiful. I chose this one because of the exploit possibility, but to all, thank you for your responses. :heart: Both worked regardless, and those solved my issue.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.