"Click to buy" script isn't working

Hello devs!

I was trying to code a “click to buy” type of thing, but my script doesn’t seem to work. The output doesn’t have an error message either. This is my script:

Script
local ClickDetector = script.Parent.ClickDetector
local BuyButton = script.Parent
local Text = script.Parent.SurfaceGui.TextLabel
local Wall = game.ReplicatedStorage.Wall4
local Player = game.Players.LocalPlayer
local GUItext = script.Parent.SurfaceGui.TextLabel.Text

ClickDetector.MouseClick:Connect(function()
	if Player.leaderstats.Money.Value >= 1000 then
		Wall.Parent = workspace
		BuyButton:Destroy()
		Player.leaderstats.Money.Value = Player.leaderstats.Money.Value - 1000
	end
end)

ClickDetector.MouseClick:Connect(function()
	if Player.leaderstats.Money.Value < 1000 then
		GUItext = "Not enough money"
	end
end)

If anyone has a solution, I would love to know! Thanks guys!

I guess it doesn’t work because you destroy the script.Parent before subtracting the money?

4 Likes

try changing to this!

		Player.leaderstats.Money.Value = Player.leaderstats.Money.Value - 1000
                BuyButton:Destroy()
2 Likes

Since the ClickDetector is the trigger, I’ll assume that its parent is a Part.

If you are using a server-sided script (Script), try using “Player” as the functions’ parameters instead of declaring it as variable since the LocalPlayer property only works on Client-sided scripts (LocalScripts).

Also, I think the Part that contains the script is being destroyed before the script subtracts the money from the player who bought the item. Try placing the “BuyButton:Destroy()” function after the line of code that subtracts the money from the purchaser.

Try using the modified code below:

local ClickDetector = script.Parent.ClickDetector
local BuyButton = script.Parent
local Text = script.Parent.SurfaceGui.TextLabel
local Wall = game.ReplicatedStorage.Wall4
local GUItext = script.Parent.SurfaceGui.TextLabel.Text

ClickDetector.MouseClick:Connect(function(Player)
  if Player.leaderstats.Money.Value >= 1000 then
       Wall.Parent = workspace
       Player.leaderstats.Money.Value = Player.leaderstats.Money.Value - 1000 
       BuyButton:Destroy()
  end
end)

ClickDetector.MouseClick:Connect(function(Player)
  if Player.leaderstats.Money.Value < 1000 then
  	   GUItext = "Not enough money"
  end
end)

I hope this helps :slight_smile:

2 Likes

Changing the leaderstats from the client won’t change anything on the server… u have to do it from a server script.

1 Like

I have the script as a Local Script, if that’s what you mean?

So, I tried what all of you guys said, moving the BuyButton:Destroy() to after the minus money line, but it still didn’t work. I tried running it in actual Roblox, not Roblox Studio. But the same thing happens. Any other solutions?

Do it in a server script not a local script

2 Likes

Ok, lemme try that. I’ll let you know how it goes.

1 Like

I just noticed that the function that processes the purchase and the function that tells the player that there’s not enough money are being triggered at the same time. I’m not sure if that may be a factor that is preventing the script to work. Try placing the line of the second function under an “else” statement placed under the “BuyButton:Destroy()” function.

Try using the modified version of the modified code (placed in a server-sided script) below:

local ClickDetector = script.Parent.ClickDetector
local BuyButton = script.Parent
--local Text = script.Parent.SurfaceGui.TextLabel --commented this since the modified GUItext variable calls the same object
local Wall = game.ReplicatedStorage.Wall4
local GUItext = script.Parent.SurfaceGui.TextLabel --deleted ".Text" from this variable

ClickDetector.MouseClick:Connect(function(Player)
  if Player.leaderstats.Money.Value >= 1000 then
       Wall.Parent = workspace
       Player.leaderstats.Money.Value -= 1000 --a shorter version of Player.leaderstats.Money.Value = Player.leaderstats.Money.Value - 1000 
       BuyButton:Destroy()

  else--if Money.Value is less than 1000
       GUItext.Text = "Not enough money"
  end
end)

Edit: I forgot to mention that a text cannot be changed if the Text property of a TextLabel is declared as a variable. Hence I called the Text property from the modified “GUItext” variable used under the “else” statement.