Hello!
This time, theres nothing wrong with my script. (SHOCKER)
But instead I was wondering…
What should I use?
Setting a bool variable to true, or returning a function to true.
Heres what I mean:
Using bool
local bool = false
local function returnBool()
if bool == true then
print("bool is true")
end
end
local function setBool()
bool = true
returnBool()
end
game.Players.PlayerAdded:Wait().CharacterAdded:Wait()
setBool()
OR, should I do something like this?
Returning true
local function setBool()
return true
end
setBool()
game.Players.PlayerAdded:Wait().CharacterAdded:Wait()
if setBool() == true then
print("True")
else
print("False")
end
Usually if you are Sending a specific type of data, you use return
Better version of your function:
local function setBool(Boolean: boolean) -- Argument assigned as a boolean
return Boolean -- Returns the Boolean
end
setBool(true) -- returns true
setBool(false) -- returns false
If you’re asking whether its better to get a value from a global or from the return of a function, return is better for some structural reasons. In your first example “bool” is a global; even though it says local, it is global relative to your functions, any of them can access it. So in your first example setBool has a Side Effect, which is setting bool to be true.
In your second example, there are no side effects and setBool is whats called a Pure Function, which means its output only depends on its inputs, or nothing, and it has no side effects. Pure functions are preferable because they make reading your code easier, since the reader does not have to keep track of which side effects have gone off as they are reading.
The simplest example of a function with side effects is print(), since it changes the state of your output window. time() is also not a pure function, as it depends on the real time, which isn’t technically an input to the function.
Since the return value of setBool is always true, in your case it is also a Constant Expression, which means it always has the same value.