How to check if a model is being touched

whats the best way to check if a model is being touched?

when i mean a model, it is a model with some parts in it

tried this but did not work

local function checkTouch(model)
	for _, part in pairs(model:GetDescendants()) do
		if part:IsA("BasePart") then
			part.Touched:Connect(function(hit)
				print("Touched")
				

Make a bounding box that contains the whole model, and connect its Touched event I suppose.

local function checkTouch(model)
    for _, part in pairs(model:GetDescendants()) do
        if part:IsA("BasePart") then
            part.Touched:Connect(function(hit)
                print("Touched")
            end)
        end
    end
end

This code should work, im assuming you didn’t add the ends on the end of the function. Considering it then moot without the ends in correct placement.

1 Like

Use the touched event functions

script.Parent.Touched:Connect(function()
script.Parent.TouchEnded:Connect(function()

This code may work, but please do not do this.

Creating many events like this can cause server lag if a model has a ton of parts in it.

Instead, consider creating an invisible hitbox part that covers the entire model and connect a part.Touched event to the hitbox part.

I get this, which in the first case is what I was going to do. However if the model is a complex shape the player may have wanted the bounding boxes to have had more precise locations. Therefore leaving me to just assume that the original code was what he intended, they just wanted to fix the errors within their original code. But nevertheless thanks for that suggestion too!

import ur roblox studio model to blender and then cut a cube in blender being the shape of the imported model then import the blender model to roblox and put it in ur roblox studio model and name it HITBOX and ur the script above this will ensure its all covered and gives it a very precise HITBOX

2 Likes

That’s a really incredibly smart I never even realised that. Gonna have to do that in my games now :broken_heart:

How about you put a Humanoid in the model and use Humanoid.Touched()?

it has 3 parts, is it a problem?

This should work. Read this, try it.

It isn’t, but it’s always better to avoid lag as much as you can.

Instead, use this code and put it in a Script in ServerScriptService.

local model = --locate ur model here

for i, v in pairs(model:GetChildren) do

    if v:IsA("Part") then
        v.Touched:Connect(function()
            -- do whatever you want here, and it'll fire once one of the parts is touched.

        end)
    end
end

I wrote it on phone, so it might give you an error. If it did, tell me!

Maybe because you are using a client script? Or like a script with RunContext to client?

1 Like