Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do i check if something in a model is touched?

How do i check if a part in a model is touched???

2 comments

0

how many parts? Be specific

0

3

3 answers

✓ Accepted answer

I made a script which i stored within the ServerScriptService and made a model which consists of 6 instances all with the same name "Part" apart from 3 which I named "Part1", "Part2" and "Part3" so i used this script to print the 3 of parts names. Remove "V.Name ~= "Part"" if you have 3 parts, also if you have more than three parts but they have different names then use this in the if statement --> "V.Name == "Part 1"" and so on, use "and" to seperate them. Here is the script:

local model = game.Workspace.Model
for _,v in ipairs(model:GetChildren()) do
	if v:IsA("Part") and v.Name ~= "Part" then
		print(v.Name)
	end
end

Follow the steps above and if you find it useful please upvote.

Touched

To test if a specific part is touched, use the Touched event:

Part.Touched:connect(function(otherPart)
	-- otherPart is the Part which touched 
end) 

To test if any part is touched in an entire Model, use GetChildren or GetDescendants to collect every member of the Model, and then connect every Part in the model

for _, part in pairs(Model:GetChildren()) do -- Iterate through the children
  if part:IsA("BasePart") then -- Make sure that the child is a part, and not something else (i.e. script)
    part.Touched:connect(function(otherPart)
      -- ...
    end)
  end
end

1 comment

0

Thank you for taking the time to answer me, but i have already found another way to do this i really appreciate it!


local Model = game.Workspace.Model


while true do

for _, Part in pairs(Model:GetChildren()) do
    if Part:IsA('Part') then
        Part.Touched:connect(function()
            print(Part.Name .. ' was touched.')
        end)
    end
end
wait()
end

There you go!

6 comments

0

Ok, thank you very much!

0

Only 1 part when touched works the others dont.

0

Oh

0

i will work on it

0

-1 for spoonfeeding. Post a better answer.

This discussion is preserved as it was. New answers, comments, and votes are closed.