Hey, XxshadowxX43. This can be very confusing for people who haven't really scripted much before. Note that I capitalized Connect, as the lowercase version was deprecated.
script is a global variable that can be accessed anywhere in the script to the script Object that is running the code.
Parent is a Property of Instances. All roblox objects and services are Instances. The Parent property points to the object that the selected object is the child of. Therefore, script.Parent is the Object right above the script in the tree.
Touched is a RBXScriptSignal, also known as an Event. In this case, it is referring to the Touched event of script.Parent. Touched is an Event of BaseParts, which includes all kinds of Parts/Bricks.
If you look it up on the wiki, the Touched event is "Fired when another object comes in contact with this object."
Every time another Object touches script.Parent, the script.Parent.Touched Event will fire.
Connect is a method of Events. Calling EventName:Connect(func) will call func each time the event is fired. In this case, doing script.Parent.Touched:Connect(touch) will make the function called touch call each time the Touched event of script.Parent fires.
Whenever an Object touches script.Parent, touch will be "fired."
Also, the first argument that the connection passes through is the Object that did the Touching, so that is how the function touch can have a hit parameter.
Other RBXScriptSignal methods can be found here
local function touch(hit)
local hum = hit.Parent:FindFirstChild("Humanoid")
if hum then
hum.Health = hum.Health / 2
end
end
script.Parent.Touched:Connect(touch)
1 comment
I am very new to scripting btw.