How do I detect when a local part touches a server part?

As the title asks, how do I do that?

I’ve tried using remote events but they need to be triggered somehow, how will I detect that trigger?

Check the network ownership of the part. if its client then its on local. This will need to be on a local script.

If you want the server to do anything with that information then you would have to use remote events to pass the information on.

2 Likes

Alright, thank you. Can I contact you if I need further assistance with this issue?

Of course, feel free to PM me if you need help.

1 Like

If the part also exists on the server, it doesn’t have to be a Localscript. A Touched event connected to a client owned part on the server will create a clientsided TouchTransmitter. The classic Linked Sword works like that.

It seems more efficient to use a LocalScript since it is more accurate.

Thats why you check the network ownership.

Network ownership does not resemble visibility to the client or server. I think you are thinking about whether the part was created by the client or by the server.

It makes no difference since the TouchTransmitter being created is identical.
edit: Unless you want to do clientsided effects which obviously need a localscript connection.

1 Like

The network owner is responsible for calculating physics, but that does not mean it is not visible to other machines. Which means that you do not need a LocalScript even if the network owner is the client.

@minimic2002 I believe you cannot set the network owner to the server if the server cannot see the part, because only the server can set the network owner. This is because the part is on the client, and only that client can see it, no one else, not the server, not the players.


So, how to solve this problem is what you originally thought to use, RemoteEvents.

Basically you would hook a Touched event to the local part in a LocalScript, then when the specified part is touched, fire the remote even and handle what you need to handle on the server:

--// Local Script

local Part = -- The local part
local ServerPart -- The specified server part

local RemoteEvent = -- The remote event

Part.Touched:Connect(function(hit) -- Fires when the part is touched
    if hit == ServerPart then -- Checks if the part it touched is the server part
        RemoteEvent:FireServer() -- Fires the remote event
    end
end)
--// ServerScript

local RemoteEvent = -- The remote event

RemoteEvent.OnServerEvent:Connect(function(player) -- When the remote event is fired, this will trigger
    -- Do what you need to do here
end)

The local part cannot fire a touch event from touching a server part, it’d have to touch another local part. Therefore, the event will not fire. That’s what I’m struggling with

You never change the network owner. Only read it. You have to use it the right way, it only checks whether its server or owned by a player. It cannot change the network ownership.

It could if the script was a local script.

Here is the same thing I sent to minimic:

In local script

CameraPart.Touched:Connect(function(player)
	print("touched")
	TouchedEvent:FireServer(player)
end)

The camera part is the player’s camera and is controlled by the user’s inputs
The ‘plates’ tweening towards the player are server parts.

Now that i know your using tweens I can tell you that the collision wont be detected with .touched. If you move a part using a script it wont register the .touched as the part has technically appeared inside it. If you want to use .touched and allow it to work you will can toggle collision quickly, when you toggle collision on then off it will allow .touched to work.

Heres a rough idea to show you (Most likely will need some changes for your use)

while true do
     if Part.Collision == true then
          Part.Collision = false
          Part.Collision = true
     end
     if Part.Collision ==false then
          Part.Collision = true
          Part.Collision = false
     end
     wait(0.1)
end

I’ve implemented this into the local script but to no avail:

spawn(function()
	while true do
		CameraPart.CanCollide = true
		wait(.1) -- I've tried wait() instead, same result
		CameraPart.CanCollide = false
		print("collide cycle")
	end
end)

If its the part is moving in open space you could use :GetTouchingParts()
The reason why I didn’t point toward this in the first place is because it has a bit more usage then .touched.

spawn(function()
	while true do
		CameraPart.CanCollide = true
                local Touching = CameraPart:GetTouchingParts()
                if Touching [1] == nil then
                
                else
                --- TouchedFunction(Touching)
                end
		wait(.1) -- I've tried wait() instead, same result
		CameraPart.CanCollide = false
		print("collide cycle")
	end
end)

This worked but only to an extent, it would fire around 20% of the time. Sometimes it even fires when the part isn’t touching the ‘plates’. I’ve made sure that it wasn’t because of the part being oversized as it is a 0.05 stud cube. I’ve also noticed a touch interest object under the part, can this be used?