How to Make GUI Button Disappear when Player has enough Coins

So I’ve been trying to, well, Make a GUI Button disappear once a player has the needed amount of coins (1000 in this case), and I don’t know how to fix it so it can work.

I have looked at the Developer Hub and asked some of my Friends but I still can’t find a solution so I’m posting it here to know what needs to be changed

local CoinAmount = 1000

local debounce = true
script.Parent.MouseButton1Click:Connect(function()
	if script.Parent:FindFirstChild("Humanoid")then 
		local player = game.Players:GetPlayerFromCharacter(script.Parent)
		if player.leaderstats.Coins.Value >= CoinAmount then 
			if debounce then
				debounce = false
	     		script.Parent.Visible = true
				if CoinAmount.Value <= CoinAmount then
					script.Parent.Visible = false
				end
			end
		end	
	end
end)

This probably looks bad cause I’m still new to scripting

Is this a local script or a server script?

player.leaderstats.Coins.Changed:Connect(function()
if player.leaderstats.Coins.Value == 1000 then
script.Parent.Visible = false
end
end)

Hold on. First of all, where is this script even located?
First, you listen if script.Parent is clicked, but then index the player via :GetPlayerFromCharacter(script.Parent), but then change the visiblity of the Gui with script.Parent.Visible = false again? What?

First things first, if you want to make changes to your GUI, you have to use a Localscript. If you change it from a Serverscript, it won’t replicate over to the client.
Second of all, if you wanna index the Player (from a Localscript!), simply use game.Players.LocalPlayer. It’s the easiest and fastest solution.

It’s located inside of the GUI Button

The debounce wouldn’t be doing anything here, instead try:

local player = game.Players.LocalPlayer
local CoinAmount = 1000
local debounce = false

script.Parent.MouseButton1Click:Connect(function()
	if player.leaderstats.Coins.Value >= CoinAmount then 
		if debounce then return end
		debounce = true
		script.Parent.Visible = false
		task.wait(3)
		debounce = false
	end
end)
1 Like