I know how to code a part to kill someone like with this function I made
script.Parent.Touched:Connect(function(hit) – If the script’s parent is touched then do something
if game.Players:GetPlayerFromCharacter(hit.parent)then – Determines if the thing that touches the barrel is a player
hit.parent:BreakJoints() – Action for function
end
end)
However, I don’t really understand what hit does and how the code knows what I am talking about.
I know what parameters do in functions that aren’t anonymous like this
function foo(bar)
print(bar)
end
foo(“bar”)
foo(“foo”)
and I know that in these functions you can replace something with a parameter and change it whenever you like but I just don’t get it with anonymous functions.
In an anonymous function, it is a function that is set to an event’s parameters which always return the fired values. You can use BindableEvents for instance that can return such parameters to simulate this, with the ability to fire the event at any time.
An anonymous function can be exactly like your example foo, but without naming the function or assigning it to a variable.
(function(bar) print(bar) end)("bar")
So that is an example of an anonymous function that mimics the code you wrote.
The Connect function will start listening for the event, and then call the function you passed to it. Sort of like this.
local function TouchConnect(boundFunc)
repeat wait() until PartIsTouched -- Obviously not how Connect really works, but it does wait for a condition.
boundFunc(PartWeTouched) -- depending on what Connect you are using, this may provide other arguments.
end
TouchConnect(function(hit) print(hit.Name) end)
Obviously the connect function has a bit more to it, but that should serve well enough to explain how anonymous functions work with them.
Additionally, to settle whether an anonymous function should be used is whenever you believe the function should be called in two different ways. Then you shouldn’t utilize an anonymous function and use an ordinary one that you can connect the function to one event or two as well as calling it manually at specific times.
The anonymous function is being passed to the Connect function and is added to a list of functions that should be called when the RBXScriptSignal is fired with whatever arguments it’s fired with.
A little oversimplified but it essentially works like this afaik.
local functionList = {}
function Connect(func)
table.insert(functionList, nil, func)
end
function Fire(var1)
for _, func in pairs(functionList) do
func(var1)
end
end
As of the kill script u wrote, the variable hit is the return value of the function. In simple words, when the touched event is triggered, the event “gives out” or returns a value (i.e. in this case the part which touched the brick). For example, if part B touches part A and you add print(hit.Name) then your script will look like this:
script.Parent.Touched:Connect(function(hit) --If the script’s parent is touched then do something
print(hit.Name) -- Just added this
if game.Players:GetPlayerFromCharacter(hit.parent)then -- Determines if the thing that touches the barrel is a player
hit.parent:BreakJoints() -- Action for function
end
end)
Then its gonna print “part B” as the variable hit is equal to the part which touched part A
Well if you didn’t understand then YouTube is always there for you (I’m not the best at explaining things, especially code, even if I have fully understood what it is)
Also try to indent your code always.