Multiple if statements firing regardless if that certain button was pressed

I made a for loop on client side that will detect if something is a text button, in side of the for loop fires a remote event.

For loop script: Client

local repstorage = game:GetService("ReplicatedStorage")

local repstorage = game:GetService("ReplicatedStorage")

for i, v in pairs(script.Parent.Parent.ButtonFolder:GetChildren()) do
	if v:IsA("TextButton") then
		v.MouseButton1Click:Connect(function()
			print("passed")
		end)
	end
end

For the other script I made an if statement if that button was pressed, but for some reason it fires both functions regardless if that specific button was pressed or not.

If statement script inside remote event: Server

local repstorage = game:GetService("ReplicatedStorage")
local button = game.StarterGui.TrailGui.TrailFrame.ButtonFolder

repstorage.TrailEvent.OnServerEvent:Connect(function()
	
	if button.Trail1.Activated then
		print("button 1 passed")
		
		if button.Trail2.Activated then
			print("button 2 passed")
			
		end
	end
end)

Here is output:
image

So the problem is that multiple if statements are firing, if I press button1 and made an if statement for it, it will activate button2 if statement, even if didn’t press button2.

Also how can I make this code shorter or look more cleaner.

1 Like

Activated is an event. In this case, you seem to be using it like a boolean.

In the for loop, when you fire the remove event, I would suggest passing v as an argument. Then, locate v inside the button folder. You will then know which button was activated, and you can do whatever you need to from there.

Example if you need it:

--client script
for i, v in pairs(script.Parent.Parent.ButtonFolder:GetChildren()) do
	if v:IsA("TextButton") then
		v.MouseButton1Click:Connect(function()
			print("passed")
            event:FireServer(v)
		end)
	end
end
--server side
event.OnServerEvent:Connect(function(plr,button)
   if button == ButtonFolder.Trail1 then
     print("button 1 passed")
   elseif button == ButtonFolder.Trail2 then
     print("button 2 passed")
end)

yes, I’m trying to see if that button has been pressed.

1 Like

Try my suggestion above and look at the example code.

out of curiosity why did you put that button parameter

In my example code, I passed v as an argument. The button parameter is v. You can use the button to identify via if then statements which button was pressed. This is an alternative to trying to use .Activated, which is used incorrectly in your code.

local repstorage = game:GetService("ReplicatedStorage")
local button = game.StarterGui.TrailGui.TrailFrame.ButtonFolder


repstorage.TrailEvent.OnServerEvent:Connect(function(plr,button)
	if button == button.ButtonFolder.Trail1 then
		print("button 1 passed")
		
	elseif button == button.ButtonFolder.Trail2 then
		print("button 2 passed")
		
	end
end)

I end up getting an error when I apply it.

Could you attach the error, please?

Change the variables, they are named incorrectly. for the if then statements, change the parameter on line 5 from button to SelectedButton or any any name, really. Then, in the if then statement, change

button == button.ButtonFolder.Trail2

to

SelectedButton = button.ButtonFolder.Trail2
local repstorage = game:GetService("ReplicatedStorage")
local button = game.StarterGui.TrailGui.TrailFrame


repstorage.TrailEvent.OnServerEvent:Connect(function(plr, Trail1, Trail2)
	
	if Trail1 == button.ButtonFolder.Trail1 then
		print("button 1 passed")
		
	elseif Trail2 == button.ButtonFolder.Trail2 then
		print("button 2 passed")
		
	end
end)

I did as you told me, the good news is that I have no errors, the bad part is that nothing is being printed in the output when i do press the buttons

Woah, this is not what I suggested. The remote event only provides two arguments, not three. I would do something like this:

local repstorage = game:GetService("ReplicatedStorage")
local button = game.StarterGui.TrailGui.TrailFrame


repstorage.TrailEvent.OnServerEvent:Connect(function(plr, SelectedButton)
	
	if SelectedButton == button.ButtonFolder.Trail1 then
		print("button 1 passed")
		
	elseif SelectedButton == button.ButtonFolder.Trail2 then
		print("button 2 passed")
		
	end
end)

hmm, it doesn’t seem to be working, also there is no errors either which is weird.

Did you pass the button parameter in the local script?

yeah it did, it works, sorry for wasting your time.

1 Like

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