#function works without passing the parameter into it?
1 messages · Page 1 of 1 (latest)
another question: why does line 8 (humanoid.Health = 0) work even though "Health" is not a child of "humanoid"?
surely it should only work if instead it was humanoid.Parent.Health = 0?, but even more confusingly, why doesnt it work anymore if it is instead this??
You put
humanoid
It's Humanoid
At least that’s what I think
no the code i showed works
humanoid is the variable for "Humanoid" which is why it works
its fine this one got solved
A okay my bad
its bc i thought "Health" here is what controls player health but apparently its actually controlled by the properties of "Humanoid"
which begs the question: what is the "Health" thing even for then??
ohhh wait you can just use it to enable/disable health?
if thats the case that makes sense i guess
but the initial question in still not sure of
** You are now Level 1! **
health is a thing on humanoids that shows how much life they have and when it hits zero they die you can change it with scripts to damage or heal and it only works on humanoids Roblox just uses it to handle life automatically
Yes
ok so i realised health script also has built in regen, so why doesnt my character regen if i set its health to like 50?
print isn't passed into the function. Neither is task.wait
The point is, every variable you reference in a function does not have to be passed to that function
It has to do with scopes and closures
Anything that exists within the function's scope can be accessed by the function. The function forms a closure around this information, keeping it in memory even when the function is called in a different script or thread
It’s about scope. Anything at the top of your script can be access by other parts of the script, but variables made inside a function can only be used within that function for example:
local players = Game:GetService("Players")
local number = math.random(1, 5)
players.PlayerAdded:Connect(function(player)
print(player.DisplayName .. "’s number is:", number)
end)```
Number and players can be accessed anywhere in the script, but player can only be accessed within the scope of the function due to it being defined within the function
Health is a property of Humanoid. If you start a playtest and go to Workspace>(Model with your username)>Humanoid, then open the properties tab, you’ll see a property called “Health”, which is why “humanoid.Health = 0” works
Health is a property of the humanoid
Also you should use Humanoid:TakeDamage(ur damage amount) if u wanna lower the players health
If u wanna kill it setting it to 0 is fine
tysm for all the help guys, i think i understand it now
so if any local variable (defined outside a function) can be accessed within a local function, whats the point of passing any variable as a parameter into a function?
also to be honest, im a bit confused about how line 16 here actually works
why is no while loop required to ensure that roblox keeps checking each frame that a collision occured?
surely from the code written, it should just check if theres a collision on the first frame, realise that there isnt and finish the script?
It can serve many different purposes. Btw the reason why people put the variables at the top is because the script can’t access anything below it, only things that are above
I’ll give an example though I’m on mobile so gimme a bit to type please
yeah i understand the second bit now
but this is really confusing me
why does the game constantly check if the part is touched even when its not on that line yet?
.Touched is an event that’s fired whenever something is touched. Think of it like a domino, it doesn’t constantly check if it’s touched before falling, it just falls whenever the touch happens
Also a little tip
You don’t have to do all those partIsTouched variables, you can connect a variable and write the logic below like so:
local serverStorage = Game:GetService("ServerStorage")
local part = serverStorage:WaitForChild("Part")
part.AncestryChanged:Connect(function()
print("Part ancestry has been changed")
end)```
hmmmm ok thanks, so the function just runs whenever the object is touched?
Here’s an example using AncestryChanged instead of Touched
So you can hopefully use this example to apply it to your own code with other events :D
what does ancestrychanged do?
Correct!!
AncestryChanged detects whenever something’s parent has changed, such as if you clone a part that’s in server storage and move it into the workspace
It can also detect if something has been deleted, in which case the parent will be “nil”. Can be useful for things like tycoon droppers (what I currently use AncestryChanged for in my project)
Ofc :D
so with code like this
if the brick is touched while the function is still running (doing the count down), what will happen?
If that happens, then the script will run again, I’d recommend adding a debounce
whats a debounce?
** You are now Level 2! **
local debounce = {}
local function exampleFunction()
—logic to find the player
if debounce[player] then return end
debounce[player] = true
—your logic here
task.spawn(function()
task.wait(time)
debounce[player] = nil
end)
end```
ohh wait i can kinda see why the pressing when the timer is running down doesnt work now
cuz then pressed never goes back to false
Here’s the general template people use for it
Oh I just gave an example
And didn’t say what it does
Debounce is a cooldown, whenever you type “local variable = {}”, {} is a list. Whenever you add a player to the debounce list and then do something like “if debounce[player]”, the game checks if the player is in said list
In the logic I wrote, “if debounce[player] then return end” will stop the rest of the function from running if the player is in the debounce list, as it returns the function, and then ends the function
sorry im still not too sure what this code does
so from what i understand the function starts by checking if the player is in the list and if it is, then the function ends?
then im not sure what it does after that
like what does task.spawn do?
Hmm the best way I can explain it is that debounce is a list of players who are on cooldown. Within your function, you wanna check if the player is debounced before running the logic, and if they aren’t, debounce them before the logic runs
Yes :D
I’m not too certain so take this with a grain of salt, but I believe that it spawns its own function isolated from the rest of the function, so that way if I do something like task.wait(), I’m not stalling the rest of the function
Like hmm let me try to visualize it
local debounce = {}
local function exampleFunction()
—logic to find the player
if debounce[player] then return end
debounce[player] = true
—your logic here
task.spawn(function()
end
—The spawned function runs separately from the main function, so that way our task.wait() doesn’t stall the "exampleFunction" from running while we perform task.wait()
task.wait(time)
debounce[player] = nil
end)```
hmm okay so i think from what you said this would allow me to ensure that the button cannot be pressed again while the wall is open, but my goal was to like just reset the timer back to 10 when the button is pressed again during the countdown
or did i misunderstand?
You don’t have to code a timer, you can just do task.wait(10)
That’s a built in timer in Luau
yeah but i also want it to output the timer as it ticks down
There’s a simpler way to do this, but if you don’t know about iterating already then I don’t wanna throw too much at you
yeah im aware of the fact that my code is probably really inefficient rn and its because im just following a tutorial and trying to understand all the code
so i tried just adding an extra line of code which sets pressed back to false to allow the first if statement to run again but then in the outputs there became multiple countdowns happening at once?
which is strange since i thought it just restarts the function
instead of like going through it multiple times at the same time
part gets touched
pressed is false so the first if statement runs
part gets touched again
but now pressed is true so the second one runs
** You are now Level 11! **
it prints multiple times because you keep creating the while loop
cuz yk
you set pressed to false again
which triggers the first if statement
about your initial question
it works because
partIsTouched is a top-level variable that you defined, which means the whole script knows what variable it is, even though its inside of a function
the reason it didnt work when you pass in partIsTouched into the function is because touchPart.Touched signal will only fire 1 argument (otherPart, hit, whatever you wanna call it) so the second argument that you pass in (partIsTouched) is gonna be nil, which ~= false
also id recommend switching to task.wait(), roblox deprecated wait() so it shouldnt be used in newer projects
its literally just 5 extra letters