Only let click function work if value is true

Hello, so, im trying to make a script where a player can press a button(gui button) and it will do a function. But i only want them to do the function when pressing the button if a certain value is equal to true. The problem is i can use this

if script.Parent.ImageButton.IfEquiped.Value == true then
      player.PlayerGui.Slots.Frame[script.Parent.ImageButton.SlotOn.Value].PressToEquip.MouseButton1Click:Connect(function()
                      --do function
      end)
end

I cant use that because the script runs as soon as the player joins the game, and when the player joins the game the value is false. The script only runs once to determine if its equal to false or not. For clarification the reason the value has to be equal to true is because the Click function wont work if its not. Ive tried countless solutions and they dont work.

I really think you should ask, on here about the main issue for why you need this setup, but just to answer this post, maybe you should check when the .Value is changed with an event.

script.Parent.ImageButton.IfEquiped.Changed:Connect(function()
  if script.Parent.ImageButton.IfEquiped.Value == true then
player.PlayerGui.Slots.Frame[script.Parent.ImageButton.SlotOn.Value].PressToEquip.MouseButton1Click:Connect(function()
                      --do function
      end)
  end
end)

I need it this way becuase i have an ability slot system, and when they equip the ability to the slot the it changes the slot on value to the slot the changed it to. So as you can see when they press the value(the slots) button, it gets the ability. But when the ability is unequipped the Slot on value goes back to nil because its not on a slot. That’s why it can only work if its equipped, so the solution you gave me doesnt work

But this could be totally different and easier if was able to use MouseButton1Clicked in an if statement

Why can’t you detect the mouse clicked, and if the mouse is clicked, check the value of the slot to see what ability is equipped?

Because its thing for my game for mobile users

what are they clicking on, on mobile a button?
if so, couldn’t you detect the button click then check the value?

They are clicking on a image button

Sorry, I probably just don’t really understand the problem. From what I see, you can let the player click a button, then check the value of the equipped ability

As i said, the value needs to be true in order for them to be able to click it

Does that mean, you don’t even want the button to be visible if the value is not true?

I explained why in that full paragraph i wrote for u

I dont want them to be able to press it if its not

If you really need it to be that way, then you could probably get what you’re hoping for by wrapping it in a loop, maybe something like while wait(1) do?

I think there is something fundamentally flawed with the design you are going with.

Do you think you could show a few screen shots of the system?

1 Like

Honestly, I’d rather you find a way to make this differently, because while this may be a solution to your problem, it’s a horifically inefficient one.

It was actually an attempt on making my game mobile, otherwise it works fine