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

Help adding an explosion to HumanoidRootPart if touched?

Asked by
FiredDusk 1466 Moderation Voter
9 years ago

Help adding an explosion to HumanoidRootPart if touched? I want when they touch the part, an explosion is added into the players HumanoidRootPart.

plr = game.Players.PlayerAdded

script.Parent.Touched:connect(function(hit)

--(/-Adding Explosion-\)--
    local Ex = Instance.new("Explosion")
    local HRP = hit.Parent:findFirstChild("HumanoidRootPart")

    hit.HPR.Ex = plr    

2 answers

Log in to vote
1
Answered by 9 years ago

In this example I will be adding a function that checks for a humanoid incase something else touches the part it won't error! You just need to add a Parent and a Position!

script.Parent.Touched:connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") ~= nil then -- if a humanoid is found this will fire!
        local Ex = Instance.new("Explosion")-- keeping your variables!
        local HRP = hit.Parent:findFirstChild("HumanoidRootPart")       
        Ex.Parent = HRP--Assigns where the explosion will be
        Ex.Position = HRP.Position--This will make the actuall explosion happen at the HumanoidRootPart
        --if you want to edit more properties just ad more to your liking!
    end
end)

Hope this helped!

Ad
Log in to vote
3
Answered by 9 years ago

I don't know if you just forgot to add it to your example code or not, but you're missing an "end)" to close the function inside that event connection.

But anyway, when creating an explosion object, it doesn't really matter where it's parented (as long as it's in the workspace), what matters is it's position. So, we could easily just assign it the same position as the humanoidrootpart like this:

plr = game.Players.PlayerAdded

script.Parent.Touched:connect(function(hit)

    local HRP = hit.Parent:findFirstChild("HumanoidRootPart")

    if HRP then -- if the HRP exists, then ...
        Instance.new("Explosion",workspace).Position = HRP.Position
        -- Instance.new returns the object we created. So, we don't have to create a variable for it if we're just setting it's parent, plus an additional property.
    end

end) -- don't forget the end

Answer this question