Script to use TextButtons not working?

This script was working the other day and now it’s not. It is supposed to fire an event corresponding with the button’s name when the player clicks. I did this so I didn’t have to have a script under each button so that it is more efficient.

Also, there are no errors in the output.

Script:

local plr = game.Players.LocalPlayer
local rep = game:GetService("ReplicatedStorage")

local function BindButtonToFunction(button, func)
	button.MouseButton1Click:Connect(func)
end

for _,v in pairs (script.Parent:GetChildren()) do
	if v:IsA("TextButton") then
		if plr.Name == rep:FindFirstChild("CurrentMaster").Value then
			BindButtonToFunction(v, function()
				local event = rep.ControlPanelEvents:FindFirstChild(v.Name)
				print("found button time to fire")
				event:FireServer()
			end)
		end
	end
end

Thanks for any help! :smiley:

2 Likes

try printing stuff inbetween all the lines and see the ones that aren’t printing. If I had to guess it could be your function you created, but I doubt that.

I’ve tried that but when I actually clicked the button nothing showed up.

my suspension is the function you created. Try doing :Connect() and see if anything changed

I don’t really know what you mean by that. I changed my script to this and it’s still not doing anything (I check and the script is still enabled)

local plr = game.Players.LocalPlayer
local rep = game:GetService("ReplicatedStorage")
local mouse = plr:GetMouse()

mouse.Button1Down:Connect(function()

for _,v in pairs (script.Parent:GetChildren()) do
	if v:IsA("TextButton") then
		if plr.Name == rep:FindFirstChild("CurrentMaster").Value then
			--BindButtonToFunction(v, function()
			v.MouseButton1Click:Connect(function()
				local event = rep.ControlPanelEvents:FindFirstChild(v.Name)
				print("found button time to fire")
				event:FireServer()
			end)
		end
	end
end
	
end)

You should try using connections Connections

local plr = game.Players.LocalPlayer
local rep = game:GetService("ReplicatedStorage")
local mouse = plr:GetMouse()

mouse.Button1Down:Connect(function()

for _,v in pairs (script.Parent:GetChildren()) do
	if v:IsA("TextButton") then
		if plr.Name == rep:FindFirstChild("CurrentMaster").Value then
			local connection
			 connection =  v.MouseButton1Click:Connect(function()
				local event = rep.ControlPanelEvents:FindFirstChild(v.Name)
				print("found button time to fire")
				event:FireServer()
                connection:Disconnect()
			end)
		end
	end
end
	
end)

Tell me if it worked. :face_with_monocle:

1 Like

Still not working :confused: It’s almost as if the buttons or script is disabled (they aren’t but they literally won’t work ;-; )

Already tried debbuging to see what’s wrong?
image
Just click in the left of the line number. Then press play and try to active your function.
Game gonna pause and you can see what’s wrong, put your mouse in a variable and you gonna see if it exists.
image
Just like this.

Try to debug yours if statements maybe there’s a value that didnt exist there.

Is that “master” value your name?

Yes, I even checked when I was testing the buttons and my name was in that “master” value.

Well, I even put a print() right after the button1down function and it didn’t print anything, so I almost feel like something is wrong with the button itself I have no idea

For text button you use something.Mousebutton1click:connect(function()
You don’t need the get mouse

Well how would the script know which button I’m pressing if I replaced the button1down function with what you said? Or am I missing something

If v:IsA(“Button”)

Then you do

V.Mousebutton1click

1 Like

Still not working :confused:

word limittt

Your overthinking this way to much. I will give you an example script in a bit,

Hold on I didn’t read your solution correctly, it works now.

The mousebutton1down function had to be right after the v:Isa like you said

Thank you sm!

lol I just made this anyways.

local plr = game:GetService("Players").LocalPlayer
local rep = game:GetService("ReplicatedStorage")

for i, v in pairs(script.Parent:GetChildren()) do
	if v:IsA("TextButton") then
		v.MouseButton1Click:Connect(function()
			if rep:FindFirstChild("CurrentMaster").Value == plr.Name then
				rep.ControlPanelEvents[v.Name]:FireServer()
			end
		end)
	end
end
1 Like

Oh thank you, I will use this instead, looks cleaner lol

I’ll make this solution for anyone else that needs this