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:
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.
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)
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)
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
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)