Spend money when you click a button

Hi Developers, I was recently doing a script to spend money when you click a GUI button. But it doesn’t work someone knows if there is an error :confused:

local MoneyNeeded = 2500

script.Parent.MouseButton1Click:Connect(function(player)

local dinero = player.leaderstats.Zeni

if dinero.Value >= MoneyNeeded then
	dinero.Value -= 2500
else
	print("You don't have enough money")



	script.Parent.Parent:Destroy()
	end

end)

I’d appreciate your help :slight_smile:

When submitting code, please include all the code in a code block. I assume this is the full code?

local MoneyNeeded = 2500
script.Parent.MouseButton1Click:Connect(function(player)
      local dinero = player.leaderstats.Zeni

      if dinero.Value >= MoneyNeeded then
           dinero.Value -= 2500
      else
	       print("You don't have enough money")
	       script.Parent.Parent:Destroy()
	  end
end)

And try to keep the indenting the same, for future

1 Like

No work :frowning:

It doesn’t work because I didn’t submit a solution. I was asking if that was the full code.
Try this though,

replace

local dinero = player.leaderstats.Zen

with

local dinero = player.leaderstats:WaitForChild("Zen")

It is not detecting the leaderstats
18:24:56.949 Players.aharoncito.PlayerGui.QuestTake.RedBean.Exit:3: attempt to index nil with ‘leaderstats’ - Server - Exit:

MouseButton1Click does not return a parameter / the player. Use LocalPlayer if that is a LocalScript. If it is a serverscript, make a remote and a localscript and fire the remote to perform your check on the serverside. The leaderstats are nil (don’t exist) because you are not finding them from the player. Player is a nil parameter.

https://developer.roblox.com/en-us/api-reference/event/GuiButton/MouseButton1Click

1 Like