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

What does "hit" do?[Solved]

While trying to figure out how to refer to the player who touched a brick I have noticed that a lot of people use "hit. " Can someone describe to me how and what it does?

1 answer

✓ Accepted answer

The "hit" event is actually the "Touched" event, and it is an event of any visible brick. It can be used for many things, but one of it's largest uses is getting the player who touched the brick. Doing that is very simple actually, there are only 3 things you need to know:

  1. Touched event
  1. FindFirstChild
  1. GetPlayerFromCharacter

Using those 3 things, we can write this code:

local Brick = game.Workspace.Part

Brick.Touched:connect(function(HitObject) --This is connecting the "Touched" event to the function in the parenthesis. The parameter "HitObject" is whatever touched the brick
	local Humanoid = HitObject.Parent:FindFirstChild("Humanoid") --Checks to see if the parent of the HitObject has a humanoid, which would indicate that it is either an NPC or a Player
	if Humanoid then --If there is a Humanoid...
		local Player = game.Players:GetPlayerFromCharacter(Humanoid.Parent) --This gets the player from the character, which is the parent of the Humanoid. If the player exists, it returns the player. If the player doesn't exist, it returns nil
		if Player then --If the Player exists...
			--Do code
		end
	end
end)

That's really it. Not much to it. Hope this helped!

2 comments

0

Wow that was a great answer. Thanks!

0

Touched events also work for invisible and can collide false brick!

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