Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Help adding an explosion to HumanoidRootPart if touched?

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

✓ Accepted answer

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!

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

This discussion is preserved as it was. New answers, comments, and votes are closed.