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

How to put a MouseButton1Click in an if/then statement?

Asked by 5 years ago
Edited 5 years ago

Hello, I'm new with coding and I could not figure out how to do something similar to if script.Parent.MouseButton1Click:connect(function() then. I have edited it to if (script.Parent.MouseButton1Click:connect()) then, but still does not seem to work. Here is my whole code, if needed. Thank's to everyone who helps!

if (script.Parent.MouseButton1Click:connect()) then
    wait(10)
local tweenservice = game:GetService("TweenService")
local fadebackground = script.Parent.Parent.Parent --change this to where your background is

local tweenInfo = TweenInfo.new(
    0.5,--time in seconds
    Enum.EasingStyle.Sine, --smooth
    Enum.EasingDirection.Out, --dont change
    0, -- dont change
    false, --reverse
    0
)

local tween = tweenservice:Create(fadebackground, tweenInfo, {BackgroundTransparency = 1})
tween:Play()
end

It seems that it is skipping the if/then, and just does the wait and rest of the script. Thanks to all who help!! Ask questions if you need more information!

0
Um a function only triggers when mousebutton is clicked. Why put an If statement? You should take out the IF statement and just leave it as a function. voidofdeathfire 148 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

MouseButton1Click is not to be associated with an if statement.

The event will trigger almost as if it is it's own if statement.

An if statement, since you are new to coding I'll give you a little run down of it.

It will basically be found usually inside an event like this, MouseButton1Click is NO exception, think of your Event while you get better at coding, think of MouseButton1Click as your own little endless if statement that will trigger at any given time the condition is met, which is to say, "Primary Mouse Button Click," or MouseButton1Click for short, but this goes for all events, not just MouseButton1Click, you will not need an if statement inside any kind of event, which is the fine line between an event and a function.

So take the if statement out, and remove the end connecting the if statement, and what is below your code will do the rest.

However, when removing it, make sure to replace your Event to work like so.

script.Parent.MouseButton1Click:Connect(function()
    --DO THIS
end)

That way it creates a new function inside of the event, allowing it to run that code! Don't think to hard on it as of now if you don't get that last part, when I was learning coding, I didn't think to hard on stuff that I just knew would work on certain events, as in, "why this," and, "why that," don't think that, just get better and learn over time as a word of advice.

0
I really appriciate you spending your time to help me!!! Also thank you for the advice!! lukasdim -3 — 5y
Ad

Answer this question