Check if Player touching a part

I know that this code:

ShopZone.TouchEnded:Connect(function(hit)
	if hit and hit.Parent == player.Character then
	end
end)

Checks for the player touching a part but is there a way to check if the player touching a part using if like if player.touching ?

3 Likes

I don’t believe there is.
However, its fairly easy to keep a variable or objectvalue holding what you are currently touching (Actually a table would be better since parts might overlap)
Just use :Touched and :TouchEnded to add to the table or remove from the table of touched parts.
Also when checking for a touch, check if the part of the character that is touching is the humanoid root part. Such as …

ShopZone.TouchEnded:Connect(function(hit)
  local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    if player and player.Character and hit == player.Character.PrimaryPart then
	 print("hit was the HumanoidRootPart")
	end
end)

I suggest this because characters can have odd body part sizes, and HumanoidRootPart is fairly regular in size, and doesn’t move with animations, so you don’t accidentally have a animation make your arm or leg hit something.

6 Likes

you can do it using a for loop and getpartinparts like this

for i, v in pairs(game.Workspace:GetPartsInPart(ShopZone)) do
if v.Parent == player.Character then
print(“Player is touching”)
break
end
end

it acts very similar to a normal if statement just remember to break it at some point if you dont want it to repeat if there are more than 1 part touching at once

2 Likes

Unfortunately if I’m not wrong, there is no such thing.

But! You can easily replicate something like this using .Touched and .TouchEnded and Variables.
Firstly, You’ll want to create a Variable named “IsTouching” for example. And you’ll want to set it to false. Don’t forget to reference the Part you are checking

And then when you do .Touched, you’ll change it to true under the .Touched function…

Then when you do .TouchEnded, you’ll change it to false.

Here’s what I’m talking about, And let’s assume only players can touch this part.

local IsTouching = false --// To check if we are touching the Part
local Part = script.Parent --// The Part we are checking

Part.Touched:Connect(function(hit) --// Part Touched!
	if game.Players:GetPlayerFromCharacter(hit.Parent) then
		IsTouching = true
		print(IsTouching)
	end
end)

Part.TouchEnded:Connect(function(hit) --// The Touch has ended!
	if game.Players:GetPlayerFromCharacter(hit.Parent) then
		IsTouching = false
		print(IsTouching)
	end
end)

I have to mention that due to character sizes and animations, If the part has CanCollide set to True while you are on the Part, Then it will keep printing different values all the time, So i suggest turning CanCollide off if possible. Or, You can do what SelDraken has done in a post above me, Where they made sure only the HumanoidRootPart was touching, Which has no animations and no different sizes.

If you want to make sure only the HumanoidRootPart is touching, do this.

local IsTouching = false --// To check if we are touching the Part
local Part = script.Parent --// The Part we are checking

Part.Touched:Connect(function(hit) --// Part Touched
	if game.Players:GetPlayerFromCharacter(hit.Parent) then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if hit == player.Character.HumanoidRootPart then
			IsTouching = true
			print(IsTouching)
		end
	end
end)

Part.TouchEnded:Connect(function(hit) --// The Touch has ended!
	if game.Players:GetPlayerFromCharacter(hit.Parent) then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if hit == player.Character.HumanoidRootPart then
			IsTouching = false
			print(IsTouching)
		end
	end
end)
3 Likes

Why not make “IsTouching” the “hit” part when Part.Touched? Like that you can detect if “IsTouching==hit” on Part.TouchEnded and only run Part.Touched if “IsTouching” is nil.

That’s if you are not detecting RootPart only.

1 Like

I don’t believe this will change much, because we need “IsTouching” to be a boolean variable.
If “IsTouching” were to be “hit”, then how would i know if the part is being touched at the moment?

1 Like

you can use “if” statement on instances and would react as if it were true.
if you use it with “nil” then it takes it as false.

just set “IsTouching” to nil when you stop touching.

1 Like

Make a BoolValue and a PlayerAdded script and make a Folder and set it player.Name then add this script to the part

local IsTouched = game.ReplicatedStorage[player.Name].Value

ShopZone.TouchEnded:Connect(function(hit)
	if hit and hit.Parent == player.Character then
        IsTouched = true
	end
end)

then in any script that is related to that one add this line:

if IsTouched == true then
       -- your lines
end
1 Like

Something like this?

local IsTouching = nil --// To check if we are touching the Part
local Part = script.Parent --// The Part we are checking

Part.Touched:Connect(function(hit) --// Part Touched!
	if IsTouching ~= nil then return end
	if game.Players:GetPlayerFromCharacter(hit.Parent) then
		IsTouching = hit
		print(IsTouching)
	end
end)

Part.TouchEnded:Connect(function(hit) --// The Touch has ended!
	if IsTouching == nil then return end
	if game.Players:GetPlayerFromCharacter(hit.Parent) then
		IsTouching = nil
		print(IsTouching)
	end
end)
1 Like

Yeah, you can use

if IsTouching then
  -- code here
end

like that and get the touching part easily. (IsTouching is the part)

3 Likes

Alrighty, Thanks for letting me know!

1 Like