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

Explain to me the "Hit" event?

Asked by
OniiCh_n 410 Moderation Voter
10 years ago

Using an explosion as the event to trigger some code. Put it in a rough example showing the event listener please.

2 answers

Log in to vote
3
Answered by
1waffle1 2908 Trusted Badge of Merit Moderation Voter Community Moderator
10 years ago
local x=Instance.new("Explosion")
x.Hit:connect(function(hit)
    hit.BrickColor=BrickColor.new("Really blue")
end)
x.Parent=workspace

Any part partially inside the BlastRadius will turn blue.

0
Now it makes sense to me. I know how I'm going to do this... OniiCh_n 410 — 10y
Ad
Log in to vote
1
Answered by
jobro13 980 Moderation Voter
10 years ago

As any event, we use :connect() to connect a function to run when this event happens. This function is passed the arguments which are provided with the event.

According to the wiki, the Explosion.Hit event passes an Instance as first argument and a Number as second argument. The first argument is the part which is hit, the second is the distance from the explosion center to that part.

Example code: remove all red bricks when an explosion fires.

function FireExplosion(position)
    local new = Instance.new("Explosion")
    new.Hit:connect(function(part, distance)
        if part and part.BrickColor == BrickColor.Red() then
            part:Destroy()
        end
    end)
    new.Position = position or Vector3.new()
    new.Parent = game.Workspace
end

FireExplosion()

Answer this question