Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Question about clicking gui buttons?

Asked by
Rhuxus 25
9 years ago

Bit stuck on this one.

What I have here is a backpack full of items which aren't going to stay the same. So is there any way to detect the name of what button you clicked on and then go from there with scripting what will happen? Or am I going to need to figure out a way to put a script into every possible button that will show up?

0
Put your current script! Normally, if you have one script that runs a loop, you can easily tell which button it is. Shawnyg 4330 — 9y

1 answer

Log in to vote
0
Answered by
M39a9am3R 3210 Moderation Voter Community Moderator
9 years ago

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 --We will say Frame is holding all the buttons, and their names are corresponding to the player's.
    v.MouseButton1Click:connect(function() --This will connect v to it's own function, and when I click a button, then inside this function I can use v to find the name or text on the button.
        --Here is where I can try to kick someone.
        game.Players[v.Name]:Kick() --Notice how I used v.Name, it should correspond to the player.
    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 --We will say Frame is holding all the buttons, and their names are corresponding to the player's.
    v.MouseButton1Click:connect(function() --This will connect v to it's own function, and when I click a button, then inside this function I can use v to find the name or text on the button.
        if v.Name == 'ff' then --If it is ff then do something.
            Instance.new('ForceField',script.Parent.Parent.Parent.Character) --This should forcefield a player. You might need more Parents the further down the script is, or if you're using a local script use game.Players.LocalPlayer.
        elseif v.Name == 'r' then --So I didn't click Forcefield, I clicked reset.
            script.Parent.Parent.Parent.Character:BreakJoints() --Kills the clicker hopefully.
        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.

0
Thanks, that'll work pefectly. Rhuxus 25 — 9y
Ad

Answer this question