-
What do you want to achieve? I want to call a function inside of an if/then statement
-
What is the issue? I’m very curious
-
What solutions have you tried so far? I tried looking it in the web, but they just only give me tutorials on “How to use an If/Then Statement.”
function test()
print("test")
end
if game.Players:FindFirstChild("xuefei123") then
test()
end
very basic example
yes, I know using userids is better than usernames
Could you try be more specific? Is this just something you want to learn or is it for a game? Please elaborate more.
Just a general tip, before posting try testing something out first. It’ll help you learning lots, and these kind if basics are core.
you cannot call a local function from outside of its block
output:nothing
and even if its global it doesnt work
output:nothing
you can however call the functions from withing the same block/if, else, elseif statement
output:
I don’t think that’s what OP meant.
I think he meant if you had a function INSIDE an if statement, and then calling it. I have no idea why he needs it, but I’m assuming he means something like this.
local i = 2
if i then
function AddTwo(i)
i = i + 2
end
end
AddTwo(i)
Ie, calling a function that is inside an if statement. I’ve honestly never seen this used, nor do I see the point of it. It would probably be better if you had the if statement with the place where you’re calling the function, rather than the place where the function is called.
A lot of people have replied to this however I would recommend you follow the advice of @xuefei123.
function DoSomething(stringToPrint)
print(stringToPrint)
end
local passIfStatement = true
if passIfStatement then
DoSomething("Function has been called from if statement")
end
NOTE (For Quote Below): This does not add 2 to the i variable, It will add 2 to the i parameter in the function however since you don’t do anything with that value, i (The Variable) will still be equal to 2
EXAMPLE:
local i = 2
if i then
function AddTwo(i)
i = i + 2
end
end
AddTwo(i)
print(i)
AddTwo(i)
print(i)
In both cases the print(i) will print 2
That been said like @oggy521 said, I’ve never seen a use for it, if you want to keep functions grouped together based on functionality or for reusability then you should look into Module Scripts.
Not sure if you tested this but if you create a function like this:
if true == true then
function SayBye()
print("Bye")
end
end
SayBye()
Then it will actually run the function
It didn’t print for you as you called SayHi before SayBye which would have stopped the script as you attempted to call a nil value
I don’t understand why you would want a global variable/function inside of a scope
I can’t get an example in mind where that would be useful for
if the if statement gets a “false”, it will just call a nil value
unless you added “else” with a different function
I’d recommend adding the if statement inside of the function
Well, tbh yes it is for a game
Also this may be another area to look at:
local Character = script.Parent
local function IsAlive()
local Humanoid = Character:FindFirstChildOfClass("Humanoid")
return Humanoid and Humanoid.Health > 0
end
if IsAlive() then
print('Character Is Alive.')
end