Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do the terms Parent, Touched, and connect work in this "Hurting Block Script"?

Asked by 7 years ago
Edited 7 years ago

So i know parent is like a step up on the tree from the part your on . But i dont completely understand how that works in this code. But LOL I dont understand Touched, and connect at all.

function touch(hit)
    hum = hit.Parent:findFirstChild("Humanoid")
    if hum~= nil then
        hum.Health = hum.Health /2
    end

end

script.Parent.Touched:connect(touch)
0
I am very new to scripting btw. XxshadowxX43 4 — 7y

1 answer

Log in to vote
1
Answered by
Validark 1580 Snack Break Moderation Voter
7 years ago

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)
    -- Declares a variable called "touch" and sticks this function in it
    local hum = hit.Parent:FindFirstChild("Humanoid")
    if hum then
        hum.Health = hum.Health / 2
    end
end

script.Parent.Touched:Connect(touch)
-- Makes it so touch is called whenever script.Parent gets touched.
Ad

Answer this question