#Trouble understanding "Parameters" in functions

5 messages · Page 1 of 1 (latest)

slow spoke
#

I have googled this question but still don't quite understand the use of a Parameter. From my understanding, its used in functions when you want to refer back to it later. Here is an example of some code using a "parameter". This code is to create a lava block that kills the player when it gets touched.

local lava = script.Parent

local function kill(lavakill)
local lavaParent = lavakill.Parent
local humanoid = lavaParent:FindFirstChild("Humanoid")
if humanoid then
humanoid.Health = 0
end
end

lava.Touched:Connect(kill)

#

Why would I need to set a parameter when I could just use the function (kill)?

spice marsh
slow spoke
spice marsh
# slow spoke I see, what would be a good example of when to use a parameter, and not to use o...

Generally speaking you will want to just use stuff that you're going to use, Some are automatically parsed by certain events, yet we can chose to simply not assign them to a variable.

e.g.

part.Touched:Connect(function()
  part.Position += Vector3.new(0,10,0)
end)
--[[
Here I only move the part up 10 studs, this can be done without
knowing who touched the part, and thus I chose to not declare
the given argument as a param (the other part that touched the part "part"
--]]

part.Touched:Connect(function(otherPart:Part) --:part is used for typechecking, just helps with autocompleting
  local plr = game.Players:GetPlayerFromCharacter(otherPart.Parent)
  print(plr.Name," has touched the part!")
end)
--[[
Here I wish to print out (or display it in some different manner) the player who touched the part, 
for this I will have to know what player has touched the part, this can be done by looking at the otherPart parameter, and thus I set it up for later use.