Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago

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

0
how many parts? Be specific Theswagger66 54 — 5y
0
3 Real_BoomyBoomYT 10 — 5y

3 answers

Log in to vote
-1
Answered by 5 years ago

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.

Ad
Log in to vote
0
Answered by 5 years ago

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
0
Thank you for taking the time to answer me, but i have already found another way to do this i really appreciate it! Real_BoomyBoomYT 10 — 5y
Log in to vote
-2
Answered by 5 years ago
Edited 5 years ago

Please provide explanation with your answers. Simply posting code does not spread knowledge of integral scripting processes which helps people understand the logic and reasoning behind your answer.

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!

0
Ok, thank you very much! Real_BoomyBoomYT 10 — 5y
0
Only 1 part when touched works the others dont. Real_BoomyBoomYT 10 — 5y
0
Oh BaconX112X 75 — 5y
View all comments (2 more)
0
i will work on it BaconX112X 75 — 5y
0
-1 for spoonfeeding. Post a better answer. DeceptiveCaster 3761 — 5y

Answer this question