How do i check if a part in a model is touched???
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.
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
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!