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

Help with "If" and "And" script?

Asked by 9 years ago

Why doesn't it let me type function click() and

it always underlines "and", or sometimes even "click()"

here is my script

function Click() and if apple == true then
    wait(5)
    script.Parent.Parent:remove()
end

end

script.Parent.MouseButton1Down:connect(Click)

thanks

3 answers

Log in to vote
2
Answered by 9 years ago

Sorry if I did give a good explanation

The and method is used in an if then end statement to check the conditions for multiple argument(s), for example, if you wanted to check if Hat and Tool exists in Workspace, then you'd do;

if game.Workspace:FindFirstChild("Hat") and game.Workspace:FindFirstChild("Tool") then --This will check the condition, or requirements before executing the code; this will check if 'Hat' and 'Tool' both exist in the 'Workspace'; the 'FindFirstChild' method will check to see if the Child exists, returns nil if not
print("Hat and Tool exists!") --This will print if both are existant in the 'Workspace'
end --This ends the code block for the 'if' statement

The or method also checks the condition for multiple arguments, but checks if condition or condition2 then code end, or, in others words, if condition is not argument1 or condition2 is not argument2 then run code end code, this can be used for multiple purposes, like to check requirements, as the and method does, or to check either requirement, here's an example;

local condition1 = 5 --The specifier is specifying '5' as 'condition1'
local condition2 = 10 --The specifier is specifying '10' as 'condition2'

if condition1 == 4 or condition2 == 10 then --This will check the requirements/conditions for the 'if' statement before executing the code; since condition1 is not equal to 4, it will then check 'condition2', which is equal to 10; if 'condition1' is equal to 4 then execute code, or if 'condition2' is equal to 10 then execute code
print("Yay! Code executed! :D") --This will print if 'condition1' is equal to 4, or 'condition2' is equal to 10
end --Ends the code block for the 'if' statement

But, if you wanted to print one Argument, then;

print(4 or 5) --Output: 4 --This print 4, but not 5 for some reason
print(4 and 5) --Output: 5 --This will print 5, but not 4 for some reason
print(1 and 2 or 3 and 4) --Output: This will print 2, but not any other number for some reason

Sadly, I can not explain why it only chooses one Argument like that. ;-; You can also use the and method and or method method in the same if then end statement, for example;

local condition1 = true --'condition1' is now identified as 'true'
local condition2 = false --'condition2' is now identified as 'false'

if (condition1 and not condition2) or (not condition1 and condition2) then --This will check to see if 'condition1' is true, and 'condition2' is false, or if 'condition1' is false' and 'condition2' is true
print("Condition1 or Condition2 is true! :D") --This will print if either one fills the requirements
end --Ends the code block for the 'if' statement

But, in short, the and method is to check if condition1 and condition2 then run code end code, and the or method is to check if condition1 or condition2 then run code end code, but, in your code, you are not using it right, you are using the and method outside an if then end statement, or, you are only using one condition, let's fix up your code;

local apple = true --'apple' is now specified, or 'identified' and 'true'

function Click() --Your function
if apple == true then --This checks to see if 'apple' is equal to true; before you were attempting to call 'apple' a nil value, which would've resulted in another error
wait(5) --This will wait '5' seconds before running the code; will fire if 'apple' is true
script.Parent.Parent:Destroy() --Will Destroy the Script's Parent's Parent is 'apple' is true
--I switched it from 'remove' to 'Destroy' because, 'remove' only reverts the Child's Parent to 'nil', 'Destroy' reverts the Child's Parent to 'nil', but also locks it from being used again
end --Ends the code block for the 'if' statement
end --Ends the code block for the function

script.Parent.MouseButton1Down:connect(Click) --Connets the event to the function; whenever your 'TextButton' or 'ImageButton is clicked, it will fire the function 'Click'

Ah, now doesn't that look much better? :) A clean code is a happy code. :D Hope this helped!

0
This is well explained TheAlphaStigma . UserOnly20Characters 890 — 9y
0
Lol, well, sometimes I think I don't explain codes too well, so, I try to explain as best I can. :) TheeDeathCaster 2368 — 9y
0
A bit of a throw-back, think of it choosing the conditions similar to this: https://scriptinghelpers.org/questions/24278/ TheeDeathCaster 2368 — 6y
Ad
Log in to vote
3
Answered by 9 years ago

You can't have the and statement with the declaration of the function try this:

function Click()
    if apple == true then
        wait(5)
        script.Parent.Parent:remove()
    end
end

script.Parent.MouseButton1Down:connect(Click)
0
that did it, Thank You So Much yogipanda123 120 — 9y
Log in to vote
0
Answered by 9 years ago

What Alpha said is correct! Although, instead of setting apple == true within the function, you could have done this:

local apple = true



function Click() 
if apple then -- this way is much better!
wait(5) 
script.Parent.Parent:Destroy() 
end 
end 



script.Parent.MouseButton1Down:connect(Click)

Answer this question