Help with textbutton

I made a i v in pairs loop to find anything that is a textbutton, then after that I fire a remote event.
On the server script I’m trying to make it check which button was activated.

I’m not sure what is going on, nothing is printing in the out put.

Client Script:

 local repstorage = game:GetService("ReplicatedStorage")

for index, value in pairs(script.Parent.Parent.Parent.ShopFrame.TrailFolder:GetChildren()) do
	if value:IsA("TextButton") then
		value.MouseButton1Click:Connect(function(SelectedButton)
			repstorage.RemoteEvent:FireServer(SelectedButton)
		end)
	end
end

Server Script:

local repstorage = game:GetService("ReplicatedStorage")
local button = game.StarterGui.Shop.ShopFrame.TrailFolder

repstorage.RemoteEvent.OnServerEvent:Connect(function(SelectedButton)
	if SelectedButton == button.Trail1 then
		print("P1")
		
		if SelectedButton == button.Trail1 then
			print("P2")
		end
	end
end)
1 Like

Make sure your script is actually reaching those connections (most likely an issue on the client), by using print or breakpoints.

Edit: MouseButton1Click does not pass any values, so you’re telling the server nothing. Use this line instead:

repstorage.RemoteEvent:FireServer(value)

I also suggest naming your remote event as its hard to tell what it does without looking at the script.

The SelectedButton exists only on the client—as do all items within your GUI—so the server receives nil. You could try naming each button what you want and then sending through the name.

Alright so, I did some testing and it works, I also change the name of the remote event also. But when I use if statements nothing happens.

Also if didn’t mention the player it wouldn’t work for some weird reason

local repstorage = game:GetService("ReplicatedStorage")
local button = game.StarterGui.Shop.ShopFrame.TrailFolder

repstorage.TrailRemote.OnServerEvent:Connect(function(player, value)
	if value == button.Trail1 then
		print("p1")
		
		if value == button.Trail2 then
			print("p2")
		end
	end
end)


Oh yes, the player argument is always first in OnServerEvent, to let you know which player has fired the remote.

If you’re sending the name of the button through the remotes then you should also be comparing the values with the buttons name in the server script.

Instead of value == button.Trail1, try value == button.Trail1.Name.

it didnt work, thanks for trying

alright so instead of using value i used in index as the parameter, I’m going to continue testing to see if all is good.

I just noticed an issue in your previous scrpt. The second if statement is inside the first one, which won’t work. Maybe try this?

--CLIENT
local repstorage = game:GetService("ReplicatedStorage")

for index, value in pairs(script.Parent.Parent.Parent.ShopFrame.TrailFolder:GetChildren()) do
	if value:IsA("TextButton") then
		value.MouseButton1Click:Connect(function()
			repstorage.RemoteEvent:FireServer(value.Name)
		end)
	end
end
--SERVER
local repstorage = game:GetService("ReplicatedStorage")
local button = game.StarterGui.Shop.ShopFrame.TrailFolder

repstorage.RemoteEvent.OnServerEvent:Connect(function(Player, SelectedButton)
	if button[SelectedButton].Name == "Trail1" then
		print("P1")
	elseif button[SelectedButton].Name == "Trail2" then
		print("P2")
	end
end)

Great news, it works.

a question, what are the brackets suppose to mean, that’s the only part I dont really understand in this script.

Good to hear it works!

You could alternatively use :WaitForChild(SelectedButton) or :FindFirstChild(SelectedButton). SelectedButton is essentially a name you’re passing through the remote. If you were to type button.SelectedButton it would error because SelectedButton isn’t a child of the TrailFolder. You’re trying to see if the name you’re passing through the remote matches with ACTUAL children in the trail folder.

interesting, thank you, I learned a lot from this one session, and how parameters work.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.