✓ Accepted answer
You can use a loop to gather all the children of what is holding the Gui buttons. So say I was making a player list with buttons corresponding to every player so I can kick them. In my hypothetical game, say I had five people (Shedletsky, Kenetec, loleris, JacksSmirkingRevenge, and myself).
I can make a Gui holding all players, and I can connect them to the MouseButton1Click event.
for _,v in pairs (script.Parent.Frame:GetChildren()) do
v.MouseButton1Click:connect(function()
game.Players[v.Name]:Kick()
end)
end
Now say I were to kick Loleris, the script will basically go, like this.
Go through all buttons > they get their own connection line > v was clicked > find what the name of the v object is > get the name and kick the corresponding player.
You can also manipulate this as well. Say you had the option to forcefield yourself (we will name the object ff), the other option to reset (we will name the object r). You can once again loop through all objects, and gather a name off of them, in order to work with your code.
for _,v in pairs (script.Parent.Frame:GetChildren()) do
v.MouseButton1Click:connect(function()
if v.Name == 'ff' then
Instance.new('ForceField',script.Parent.Parent.Parent.Character)
elseif v.Name == 'r' then
script.Parent.Parent.Parent.Character:BreakJoints()
end
end)
end
Tried it? Not working? Make another question and post the script. Hopefully this answer helped, if you need further explanation, then let me know by using the comments.
1 comment
Put your current script! Normally, if you have one script that runs a loop, you can easily tell which button it is.