How i can make a script that connect a part with a other part like i said on title,
how i make, if this part touch this other part then… = on script
How i can make a script that connect a part with a other part like i said on title,
how i make, if this part touch this other part then… = on script
local part1 = -- Path in workspace to your first part
local part2 = -- Path in workspace to your second part
part1.Touched:Connect(function(hit)
if hit.Parent.Name == part2.Name then
print(part1.Name .. " hit " .. part2.Name "!")
end
end)
Why are you checking the parent’s name? You could check the part’s name, or check the part itself
Correct function (with tbe variables @MJTFreeTime used)
part1.Touched:Connect(function(hit)
if hit == part2 then
print(part1.Name .. " hit " .. part2.Name "!")
end
end)
Checking the part’s parent would only be useful in the event that you need to check if the one touching the part is a character.
Ah okay, I was in the thought process used when working with players, not singular parts (whoops lol). And also, I’m simply wary about using ==
with instances, as I don’t know how the Roblox engine handles boolean comparisons between them; thus, assuming unique names to all parts, comparing names was simply a safer bet (but it probably works fine then).
Here’s a way of doing it that will work for a near infinite amount of parts:
local parts = {
part1 = -- Path in workspace to your first part
part2 = -- Path in workspace to your second part
part3 = -- Path in workspace to your third part
-- etc.
}
local debounce = false
local partBeingTouched
local partTouched
while true do
for i, part in pairs(parts) do
local touchingParts = v:GetTouchingParts()
for j, touching in pairs(touchingParts) do
if touching == part and not debounce then
print(part.Name .. " touched " .. touching.Name "!")
partBeingTouched = part
partTouched = touching
debounce = true
elseif touching == part and debounce and (touching ~= partTouched or part ~= partBeingTouched) then
print(part.Name.. " touched " .. touching.Name "!")
partBeingTouched = part
partTouched = touching
else
debounce = false
end
end
end
end
This way, let’s say OP has 1000 parts and wants to check for any time any of those parts collide with each other. It will loop through all the parts in the array, then loop through all the colliding parts and compare them to the parts in the array. I also included a debounce so that it won’t print 100000000 times a second (exaggeration); however, I also included simple logic that will make the script print again if the parts switch to colliding with different parts, so that it won’t miss any changes. It might be overkill, but perhaps it can help in learning different methods as well.
I hope I helped! And feel free to point out anything wrong with the above.
Comparing a variable to an instance works perfectly well. You can test this by putting this code inside a new baseplate:
local Part = workspace.Baseplate if workspace.Baseplate == Part then print("True") end
Ah okay, thanks for the clarification; though I already accepted that method in my previous post, and used it in the code.