How would I check if a character pressed multiple guis and save that data? basically, when a character wants to make a skill, they’ll use that gui, with that after they press a certain button they’ll have a skill pre-made but I want to know how I could get up to a max of skills to be selected to be brought together into one skills and save which skills you pressed to combine (the skill combinations are made already) current script I got going for me, at the moment.
--#Amount of Skills
local SPAmounts = {Skills["Punch"], Skills["Kick"], Skills["SpinKick"], Skills["Elbow"]}
function SkillChecker()
end
local function MultipleSelection(Clicked, AoS)
for i, Skills in pairs(MCInside:GetChildren()) do
table.insert(SkillClicked, Skills[i])
Clicked = true
if AoS == 3 then
warn("You cannot go further than this.")
return
end
end
end
The general issue I am facing is knowing how I would get the script to acknowledge that it cannot press the same skill more than once, and that the script is pressing a different skill, and placing it into the table.
You may use a boolean values to know which buttons have already been pressed, and configure the value in the moment that the button has pressed, like this:
local button01V = false
local button02V = false
local button03V = false
local button01 = playerGui.ScreenGui.Button --the button value
button01.Activated:Connect(function()
if button01V == false then
--the actions to be carried out
button01V = true
end
end)
Yeah, but with this I have to reference each and every button that is planned to be in there. Isn’t there a better way to check if you didn’t press the same button?
With the code that I gave you, you already know which button has been pressed, because their respective boolean value would be true; if the value is true, the button already has pressed; you can create one bool value for each button, and connect the function that checks if the bool value is true or false in the ‘Activated’ event of each button.
Do you still need another way for this?
Here is a very basic example of a dictionary based approach
local buttonsList = {} --something like script.Parent.Buttons:GetChildren() so long as if all children are buttons.
local activeButtons = {}
local function onButtonClick(button)
activeButtons[button] = (activeButtons[button] == nil) or nil --This flips a buttons state, if it was active, it now isn't and vice versa.
--The 'or nil' here might be a tad confusing. Basically a value is removed from a dictionary if it's set to nil, but will still be technically in the dictionary if set to false. So when the parenthesis comparison evaluates to false, the or nil changes the value to nil instead of false. But when the comparison is true, it skips the nil and is set to true.
end
for _, b in pairs(buttonsList) do
b.Activated:Connect(function() onButtonClick(b) end)
end
--So to check if a button has already been pressed you would do the following
local isPressed = activeButtons[buttonToCheck]
The code above will essentially turn on and off the values whenever you click a button. You can check if a value is on simply by doing activeButtons[button]. If that returns true then it’s active. If it’s nil it’s inactive.
Here is an example of the same using a normal list
local buttonsList = {} --something like script.Parent.Buttons:GetChildren() so long as if all children are buttons. This creates the .Activated events as well as some extra stuff for them
local activeButtons = {}
local function onButtonClick(button)
local index = table.find(activeButtons, button)
if index then
table.remove(activeButtons, index)
else
table.insert(activeButtons, button)
end
end
for _, b in pairs(buttonsList) do
b.Activated:Connect(function() onButtonClick(b) end)
end
--So to check if a button has already been pressed you would do the following
local isPressed = table.find(activeButtons, buttonToCheck)
Yeah it doesn’t matter how you supply it, but it needs to be whatever button you’re checking. So you could make it as a parameter in a function or you could just supply it directly.
For some reason, when I try to use it, the output will tell me that isPressed = nil
for _, b in pairs(PlayerGUI:WaitForChild("MoveCre"):WaitForChild("OutSideMC"):WaitForChild("MoveCreation"):GetChildren()) do
b.MouseButton1Click:Connect(function() onButtonClick(b) buttonToCheck = onButtonClick(b) end)
end
local isPressed = activeButtons[buttonToCheck]
print(isPressed)
local function FixUpGang(button)
if isPressed and #activeButtons < MAX_SELECTION then
button.BorderSizePixel = 1
elseif not isPressed and #activeButtons:GetChildren() > MAX_SELECTION then
button.BorderSizePixel = 0
error("auggh")
end
end
I don’t really know how to directly fix it even though I somewhat know the issue.
For your use case I would likely recommend the second example I gave (I’m unsure which you used). This problem is hard for me to actually help with because there is a lot of room for guesswork on my end. Like I don’t know under what conditions FixUpGang is called. I’m assuming from context that it merely redecorates the button based on whether it’s selected or not. Since it redecorates it’s best to call it at the end of the input button so we can ensure order rather than bind it elsewhere. It’s worth noting that I can’t test it, so don’t expect it to be 100% error free. And of course with lack of knowledge of the whole system, I was only able to build the system the way I assumed you were building it.
Code
local buttonsList = PlayerGUI:WaitForChild("MoveCre"):WaitForChild("OutSideMC"):WaitForChild("MoveCreation"):GetChildren()
local activeButtons = {}
local function isPressed(button)
table.find(activeButtons, button)
end
local function canButtonBePressed(button)
--This whole function can be written as 'return isPressed(button) or #activeButtons < 3'
--I wrote the if statements out as it might be easier to understand that way.
if isPressed(button) then
return true --This is a click to disable it, so it's allowed regardless of the amount of activeButtons
else
if #activeButtons < 2 then --Since we are activating it, we need to make sure that there are not already 3 buttons pressed
return true
else
return false
end
end
end
local function FixUpGang(button)
if isPressed(button) then
button.BorderSizePixel = 1
else
button.BorderSizePixel = 0
end
end
local function onButtonClick(button)
if not canButtonBePressed(button) then return end --terminate function if canButtonBePressed returns false or nil
local index = table.find(activeButtons, button)
if index then
table.remove(activeButtons, index)
else
table.insert(activeButtons, button)
end
FixUpGang(button) --call the function to redecorate the button
end
for _, b in pairs(buttonsList) do
b.MouseButton1Click:Connect(function() onButtonClick(b) end)
end
For some reason, when I do this, nothing works. I edited my previous script further, and it works to an extent until we reach back to
Which is quite annoying because it’ll work for the first button pressed, but if another button is lit up, it’s added to the table even though I swore it already existed prior.