Button doesn't work when pressed

Hi! I want to create a shop system. Sadly, when I press the buy button, nothing really happens. I put a print() inside the funcion that runs when clicked but even that doesn’t work. I’ve tried putting prints and debugging, as seen in the script. Please help!

--StarterGui.BuyTime.BuyButton.LocalScript
local RepStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local LocalPlayer = Players.LocalPlayer
local PlayerGui = LocalPlayer.PlayerGui
local leaderstats = LocalPlayer.leaderstats
local cornStat = leaderstats and leaderstats:FindFirstChild("corn")
local timeupgStat = leaderstats and leaderstats:FindFirstChild("timeupg")
local SurfaceGui = PlayerGui.SurfaceGui
local TimePrice = RepStorage:WaitForChild("TimePrice")
local TimePriceLabel = PlayerGui.BuyTime:WaitForChild("TimePriceLabel")

script.Parent.Activated:Connect(function()
	print("works")
	if cornStat.Value >= TimePrice then
		cornStat.Value = cornStat.Value - TimePrice
		TimePrice.Value = TimePrice.Value + TimePrice.Value * 0.1
		timeupgStat.Value = timeupgStat.Value + 1
		TimePriceLabel.Text = tostring(TimePrice.Value) .. "Corn"
	else
		print("broke")
	end
end)

The code is in the button that says “Buy”

If its a button and you want the button to fire that code when clicked, you use: script.Parent.MouseButton1Click instead of Activated.

1 Like

Something important I notice here is
Youre changing the player stats on client, meaning an exploiter can easily ruin and change it. Use remotes to handle it on server with sanity checks.

1 Like

Already tried that

I don’t know how to do that yet (rookie here lol)
I’ll add an image of what the surface gui looks like (the code is in the button that says “Buy”)

Does it print any errors in the output when you press the button in testing mode?

No, it doesn’t. I’ve been trying to look for one but to no avail.

Can you make the button a variable just incase its picking up something else?

I tried it, but it doesn’t work. I still can’t press the button

You could also try using :WaitForChild() to wait for the button to load.

Example of what I’m talking about:

local Button = script.Parent:WaitForChild("Button")-- wherever the button is located

script.Parent.MouseButton1Click:Connect(function()
    print("Button pressed")

    -- put action below the print
end)

I did that in this line

local BuyButton = PlayerGui.BuyTime:WaitForChild("BuyButton")

Since it’s a button you need to use MouseButton1Click instead of Activated.

You also need to make it a normal script instead of a LocalScript (if you plan on having it in the Workspace). LocalScripts can only be run from the client.

You can use a LocalScript if you need it for LocalPlayer but you will have to move the SurfaceGui object to StarterGui or the player’s PlayerGui, then set the Adornee property of the SurfaceGui to the part you want it on.

I would check the Zindex of the button and make sure nothing is in front of it.

I put MouseButton1Click earlier, I said that the localscript is inside of the button, and I had already put the gui into StarterGui and then set the adornee from the start.

Checked, nothing is in front. Still doesn’t work

local RepStorage = game:GetService("ReplicatedStorage")
local Players = game.Players

local plr = Players.LocalPlayer
local leaderstats = LocalPlayer:WaitForChild("leaderstats"
local cornStat = leaderstats:WaitForChild("corn")
local timeupgStat = leaderstats:WaitForChild("timeupg")
local TimePrice = RepStorage:WaitForChild("TimePrice")
local TimePriceLabel = script.Parent.Parent:WaitForChild("TimePriceLabel")

script.Parent.MouseButton1Click:Connect(function()
	print("works")
	if cornStat.Value >= TimePrice then
		cornStat.Value = cornStat.Value - TimePrice
		TimePrice.Value = TimePrice.Value + TimePrice.Value * 0.1
		timeupgStat.Value = timeupgStat.Value + 1
		TimePriceLabel.Text = tostring(TimePrice.Value) .. "Corn"
	else
		print("broke")
	end
end)
1 Like

Well, I can get the timeupgstat now. Sadly, it doesn’t take my money and it displays the price as 0… so…
Edit: nvm i forgot to change something lol