What's the most efficient way to find if specific parts have been touched by a player?

So, I couldn’t find close to any info of this on the devforum. I may have just been looking in the wrong places, however…

Does anybody know the most efficient way to reduce memory and lag, whilst checking if a specific part has been touched or is being touched? My current method uses a good bit a memory, as it runs on the client and checks if the part was touched, and if it was, it uses a RemoteEvent and sends a signal to the server. My thoughts were that there was a better option here, but I’m not sure.

I’m pretty sure you can use a for i,v in pairs() loop, and just check if the variable was touched, it runs for each part.

A few things, I guess it is good to keep memory down; however, from my experience, high memory doesn’t really affect gameplay. The other thing is, can you provide some code? I can think of many solutions, but I don’t know which would work for you if I’m not sure what you are attempting to do.

Thanks for the tip about memory. I always just try to tend to keep it down as much as I can for the sake of lower end devices. As far as the code goes, here you go

Local Script

HumanoidRootPart.Touched:Connect(function(part)  --- Getting every part that touches the player/ Everytime the player touches a part
    if part ~= nil then --- Just checking if it exists
		if part:FindFirstChild("Infect") then --- Find a value inside the part
			game.ReplicatedStorage.Infect:FireServer(Player, Character, Humanoid, part.Name) --- Fire the server
		end
	end
end

The server script doesn’t really matter to this though, its really just a receiver.

Yeah, in your case I would recommend @Fvrenight’s solution and loop through all the parts and check if the player touches the part.

Then should I manage that through the client or the server? I can see both solutions working but what would it be better to run it on?

It depends on what you’re trying to do, if you’re trying to get it seen by everyone, and also the server, then replicate it on the server. If it’s client sided, replicate it onto the client.

Well, you are trying to save memory, which is on the client, so you might do this on the server.

1 Like

Even if I ran it on the client, it would be sending to the server, so the server is ultimately the best option. Thanks to both of you!

1 Like

ServerScriptService

for _,Parts in pairs(workspace:GetChildren()) do

	
	if Parts:IsA("Part") then
		Parts.Touched:Connect(function()
			
			print((Parts))

		end)
	end
end


1 Like