I want to be able to call which button was pressed (which would be v in the code) under a list of buttons, all of which are a child to a single frame.
Right now, it only detects when one of the buttons in my list is activated. I’d like to know when any of the buttons under the frame are activated
I’ve tried looking around for solutions and couldn’t find one that would work for this specifically.
Thank you.
local RS = game:GetService("ReplicatedStorage")
local CharRemote = RS:WaitForChild("CharSwapEvent")
local CharFunc = RS:WaitForChild("CharSwapFunc")
local ButtonFrame = game.StarterGui.CharacterSelectGUI.ButtonFrame
local CharacterFrame = ButtonFrame.CharacterFrame
local Characters = RS.Characters
for i, v in ipairs(script.Parent:GetChildren()) do
if v:IsA("ImageButton") then
v.Activated:Connect(function()
for _, character in Characters:GetChildren() do
if v.Name ~= character.Name then return end
CharFunc:InvokeServer(character)
end
end)
end
end
Just print the name of the button when it is clicked
function Notify(button)
print('Button '..button..' was activated')
end
local RS = game:GetService("ReplicatedStorage")
local CharRemote = RS:WaitForChild("CharSwapEvent")
local CharFunc = RS:WaitForChild("CharSwapFunc")
local ButtonFrame = game.StarterGui.CharacterSelectGUI.ButtonFrame
local CharacterFrame = ButtonFrame.CharacterFrame
local Characters = RS.Characters
for i, v in ipairs(script.Parent:GetChildren()) do
if v:IsA("ImageButton") then
v.Activated:Connect(function()
Notify(v.Name)
for _, character in Characters:GetChildren() do
if v.Name ~= character.Name then return end
CharFunc:InvokeServer(character)
end
end)
end
end
@MysteryX2076’s code helped me figure out that the code to find which button press was working. The issue is that when the loop to find which character name matches the button name, only one of the character button events work. Whichever button works seems to be random. I can’t explain it any better, so here’s a video of what i mean. Sometimes the print that runs is only for Default.
Here’s also the code that the server runs when the remote fuction is fired.
remoteFunc.OnServerInvoke = function(player, character)
player:SetAttribute("SetCharacter", character.Name)
print(player:GetAttribute("SetCharacter"))
end
I can draw only one conclusion from your video, the ‘Default’ character doesn’t even exist in the RS.Characters, go check whether it exists and if it is check the spelling
It does exist. Sorry, I should’ve elaborated more.
Sometimes it also prints out “Default” normally. It seems to be random which one works. I’m not sure why it does this.
Firstly, you are not supposed to use return if you want to iterate all characters using a loop in this case. Use continue instead if you don’t want the loop to run any code after the statement.
This worked exactly as I had intended. Thank you guys for the help, I really appreciate.
switching to then continute end and adding a return right after invoking the remotefunction on the client. Thank you , I’ll keep it in mind for the future.