Why won't MouseButton1Click work on my TextButton?

The issue is that my LocalScript doesn’t detect when the TextBouton is clicked.

How do I know it doesn’t detect?
because I use a Print(), and it doesn’t Print()

-- this is the content of my LocalScript
game.Players.PlayerAdded:Connect(function(player)
	local playerLevel = player.leaderstats:WaitForChild("Level")
	local shop = game.StarterGui:WaitForChild("Shop")
	local primaryShopButton = shop:WaitForChild("PrimaryShopButton")
	local frameLevel = script.Parent:WaitForChild("FrameLevel")

	primaryShopButton.MouseButton1Down:Connect(function()
		print("Click")
		wait(.1)
		for _, v in pairs(script.Parent:GetDescendants()) do
			if v:FindFirstChild("Level") then
				
			elseif v.Value >= playerLevel.Value then
				frameLevel.Visible = false
			else
				frameLevel.Visible = true
			end
		end
	end)
end)

that’s the TextBouton

That’s where the LocalScript its placed in my Explorer
Screenshot (174)

That’s the script in format image (for people who want the script in image)

1 Like

Get rid of this. It won’t fire. The player already is added if the LocalScript exists.

1 Like

This is because you’re using a local script w/ game.Players.PlayerAdded. That event simply doesn’t fire on a local script. Simply remove the “.PlayerAdded” event entirely, as the local script will automatically run right when the player joins.

2 Likes

ok
but how I can get the player?

First of you dont need the PlayerAdded Event in a Localscript. The Player is game.Players.LocalPlayer

1 Like
local player = game.Players.LocalPlayer

In LocalScript instances

1 Like

Just do game.Players.LocalPlayer (only works for local scripts)

1 Like

ok now the script look like this
what next?

local player = game.Players.LocalPlayer
local playerLevel = player.leaderstats:WaitForChild("Level")
	local shop = game.StarterGui:WaitForChild("Shop")
	local primaryShopButton = shop:WaitForChild("PrimaryShopButton")
	local frameLevel = script.Parent:WaitForChild("FrameLevel")

	primaryShopButton.MouseButton1Down:Connect(function()
		print("Click")
		wait(.1)
		for _, v in pairs(script.Parent:GetDescendants()) do
			if v:FindFirstChild("Level") then
				
			elseif v.Value >= playerLevel.Value then
				frameLevel.Visible = false
			else
				frameLevel.Visible = true
			end
		end
	end)

for info it still don’t print() I test it

Maybe you also want to add this after the player declaration repeat wait() until player:FindFirstChild(“leaderstats”)

The Shop is not in StarterGui, its in player.PlayerGui

like this?
local shop = player.PlayerGui:WaitForChild(“Shop”)

There’s a possibility that one of your wait variables are producing infinite yield, and correct me if I’m wrong, but the script won’t continue if it’s waiting permanently.

Yes. But also maybe you want to use a WaitForChild for the playergui. Its more safe for slower loading clients.

1 Like

no I don’t think
is it producing infinite yield?

Here is your problem. You are referencing StarterGui when you need to reference the player’s PlayerGui, which can be done by changing this variable to:

local shop = game.Players.LocalPlayer.PlayerGui:WaitForChild("Shop")

ok everything its fine
but…
its sending me a error that “Value is not a valid member of Frame”
The part of the script

		for _, v in pairs(script.Parent:GetDescendants()) do
			if v:FindFirstChild("Level") then
				
			elseif v.Value >= playerLevel.Value then

This error happens because Value isn’t a property of a Frame, which can be avoided by checking if it’s a frame or not and THEN running the line of code.

1 Like

The elseif is causing the issue. Just remove the elseif and only write if. If it cant find the Level Object, then its trying to use v.Value which doesnt exist on a Frame.

2 Likes

new error that I really don’t understand
error "Value is not a valid member of Part “Players.YasinOfficiel.PlayerGui.Shop.PrimaryMainFrame.PrimarySafeArea.PrimaryItemFrame.Item1.VPF.PistolHandle”

I am finding the first child “Level” and its a Value, but the script is trying to get a value from the Parent (PistolHandle) of the “Level”

primaryShopButton.MouseButton1Down:Connect(function()
		print("Click")
		wait(.1)
		for _, v in pairs(script.Parent:GetDescendants()) do
			if v:FindFirstChild("Level") then

Can you highlight the exact line where the error occurs?

1 Like
local player = game.Players.LocalPlayer
local playerLevel = player.leaderstats:WaitForChild("Level")
local playerGui = player:WaitForChild("PlayerGui")
local shop = playerGui:WaitForChild("Shop")
local primaryShopButton = shop:WaitForChild("PrimaryShopButton")
local frameLevel = script.Parent:WaitForChild("FrameLevel")

	primaryShopButton.MouseButton1Down:Connect(function()
		print("Click")
		wait(.1)
		for _, v in pairs(script.Parent:GetDescendants()) do
			if v:FindFirstChild("Level") then
				
			***if v.Value >= playerLevel.Value then***
				frameLevel.Visible = false
			else
				frameLevel.Visible = true
			end
		end
	end
end)