Put touched event in a if statement

Is it possible to have something like

if part.Touched by player then
    do something
end

part.Touched:Connect(function(Hit)
    if Hit.Parent:FindFirstChild("Humanoid") then --// checks if its a player
       print("Part touched by "..Hit.Parent.Name) 
    end
end)

1 Like

Makes sense, I have been also asking myself that type of question

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local shootEvent = Instance.new("RemoteEvent", ReplicatedStorage)
shootEvent.Name = "shootEvent"

local function onShootEventFired(player, shotPower)
	print(player.Name .. " wants to shoot the ball with " .. tostring(shotPower) .. " power")
end

shootEvent.OnServerEvent:Connect(onShootEventFired)

how do i add it into the onShootEventFired?

If you want to activate something when a player touches it, you can do this:

Let’s say you have a part with a .Touched event attached to it.

When a player touches that part, the touched event will get fired with the parameter being which instance inside the players character touched our part.

What you can do is check if the Parent property of the instance that touched the part is a character model belonging to the player using Players:GetPlayerFromCharacter(charactermodelhere)

Here’s an example with an imaginary part inside Workspace:

local Players = game:GetService('Players');
local part = workspace.Part;

part.Touched:Connect(function(instance)
	if (Players:GetPlayerFromCharacter(instance.Parent)) then
		print("Touched by player!");
	end
end)

Now, what if the part gets touched by something inside of a players character model that isn’t in the root of the character model? You could use a for loop on Players that would check if the instance is a descendant of each players character model.

local Players = game:GetService('Players');
local part = workspace.Part;

part.Touched:Connect(function(instance)
	for _,plr in pairs(Players:GetPlayers()) do
		if (instance:IsDescendantOf(plr.Character)) then
			print("Touched by player!");
			break;
		end
	end
end)

Hope this is of help, my pleasure to help further.

I dont understand what u mean by that, Can u give more info about it Doc?

inside the onShootEventFired, I want to put in if player touching ball.

So inside the function you want to put an If Statement that checks if a player is touching the ball?

yes

need chars this is so sad how is it not 30 yet

is there a way to put it in a if statement?

I think maybe Gettouchingparts could do?

ANd why aren’t u using the TOuched event?

because i only want it to check if its being touched when the shoot event is called

U mean when the function is called?

-- For specific player.

local is_touching = false
local touched_parts = part:GetTouchingParts()

for _, v in touched_parts do
	if v.Parent == player.Character then
		is_touching = true
		break
	end
end

if is_touching then
	-- do something
end


-- For all players.
local Players = game:GetService("Players")

local is_touching = false
local touched_parts = part:GetTouchingParts()

for _, v in touched_parts do
	if Players:GetPlayerFromCharacter(v.Parent) then
		is_touching = true
		break
	end
end

if is_touching then
	-- do something
end




2 Likes

it doesnt work for me. this is my current code (no error either)

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local shootEvent = Instance.new("RemoteEvent", ReplicatedStorage)
shootEvent.Name = "shootEvent"

local function onShootEventFired(player, shotPower)
	print(player.Name .. " wants to shoot the ball with " .. tostring(shotPower) .. " power")
	local is_touching = false
	local touched_parts = game.Workspace.Ball:GetTouchingParts()

	for _, v in touched_parts do
		if v.Parent == player.Character then
			is_touching = true
			break
		end
	end
	print(is_touching)
	if is_touching then
		print("player touching ball!!!")
	end
end

shootEvent.OnServerEvent:Connect(onShootEventFired)


Try printing touched_parts, if the table is empty, the ball is currently not touching anything.

it wasn’t empty. even when i anchor the ball and stand on it and click, nothing changes.

Do this:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local shootEvent = Instance.new("RemoteEvent", ReplicatedStorage)
shootEvent.Name = "shootEvent"

local function onShootEventFired(player, shotPower)
	print(player.Name .. " wants to shoot the ball with " .. tostring(shotPower) .. " power")
         part.Touched:Connect(function(instance)
	if (Players:GetPlayerFromCharacter(instance.Parent)) then
		-- Do code here
	end
end)
end

shootEvent.OnServerEvent:Connect(onShootEventFired)

Okay, after a bit of testing I found out that GetTouchingParts does not detect parts that has CanCollide false. The player’s character’s legs and arms are by default CanCollide off. I found a workaround to this problem.

	local character = player.Character or player.CharacterAdded:Wait()
	
	local is_touching = false
	
	local connection = ball.Touched:Connect(function() end)
	local touched_parts = ball:GetTouchingParts()
	connection:Disconnect()
	
	for _, v in touched_parts do
		if v.Parent == character then
			is_touching = true
			break
		end
	end
	
	print(touched_parts)
	print(is_touching)

	if is_touching then
		-- do something
	end