#function works without passing the parameter into it?

1 messages · Page 1 of 1 (latest)

celest carbon
#

why does the code shown in the image work even though "partIsTouched" isnt passed into the function and when i do pass it into the function it doesnt work?

#

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??

knotty mantle
#

humanoid

#

It's Humanoid

#

At least that’s what I think

celest carbon
#

no the code i showed works

#

humanoid is the variable for "Humanoid" which is why it works

knotty mantle
#

A okay my bad

celest carbon
#

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

celest carbon
obtuse monolithBOT
#

studio** You are now Level 1! **studio

knotty mantle
#

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

celest carbon
# knotty mantle 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?

knotty mantle
#

Hmmm

#

Maybe for the RegenerationRate

#

That's make that the healing was slow

forest herald
#

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

dark parcel
#

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

dark parcel
manic light
#

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

celest carbon
#

tysm for all the help guys, i think i understand it now

celest carbon
celest carbon
#

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?

dark parcel
#

I’ll give an example though I’m on mobile so gimme a bit to type please

celest carbon
celest carbon
#

why does the game constantly check if the part is touched even when its not on that line yet?

dark parcel
#

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)```
celest carbon
dark parcel
#

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

celest carbon
#

what does ancestrychanged do?

dark parcel
#

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)

celest carbon
#

wow interesting

#

thank you for the help

#

i really appreciate it

dark parcel
celest carbon
#

if the brick is touched while the function is still running (doing the count down), what will happen?

dark parcel
celest carbon
#

whats a debounce?

obtuse monolithBOT
#

studio** You are now Level 2! **studio

dark parcel
#
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```
celest carbon
#

cuz then pressed never goes back to false

dark parcel
#

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

celest carbon
#

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?

dark parcel
#

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

dark parcel
# celest carbon like what does task.spawn do?

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)```
celest carbon
#

or did i misunderstand?

dark parcel
#

That’s a built in timer in Luau

celest carbon
#

yeah but i also want it to output the timer as it ticks down

dark parcel
celest carbon
#

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

arctic flower
obtuse monolithBOT
#

studio** You are now Level 11! **studio

arctic flower
#

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

arctic flower
# celest carbon why does the code shown in the image work even though "partIsTouched" isnt passe...

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