Table.find not working with GetPartsInPart

I’m trying to make a puzzle game where you push a cube onto a button but the button can’t find the cube. It may have something to do with the quotation marks but idk.

Code:

local Button = script.Parent.Button
local Hitbox = script.Parent.Hitbox
local Door = script.Parent.Door

local TweenService = game:GetService("TweenService")
local Room = game.Workspace.Rooms.Room1

while task.wait(0.5) do
	local TouchingParts = game.Workspace:GetPartsInPart(Hitbox)
	print(TouchingParts)
	print(table.find(TouchingParts, "Cube"))
end

Output:

  15:25:27.612   ▼  {
                    [1] = Cube
                 }  -  Server - Script:10
  15:25:27.617  nil  -  Server - Script:11
1 Like

Touching parts returns an array with instances, and you are trying to find the part by a string which won’t work. Try to index that with an instance

that wont work because you cant index something from a table with instances with a string

like @ItsSitOMG you could also try indexing with a instance example

for i, v in TouchingParts do
    if v == cube then -- this should be a Instance
        print("cube found")
    end
end

im not sure if this method is the most efficient but you can do something like this

for i, v in TouchingParts do
    if v.Name == "Cube" then
       print("cube found")
       -- you can also break the loop if you found a instance named cube.
    end
end

This works but is there a way to deactivate the button once the cube is removed?

1 Like

sure!

here’s a example of how you can achieve that

you should save the cube in a variable once it has been found

local cube = Instance -- you should save the cube once the cube has been found

local connection = cube:GetPropertyChangedSignal("Position"):Connect(function()
   local touchingparts = workspace:GetPartsInPart(Hitbox)

   local cube

   for i, v in touchingparts do 
      if v.Name == "cube" then
          cube = v
          break
      end
   end
       
    if cube == nil then
      deactivate() -- assuming you have a deactivate function you can deactivate the button if cube is not found
    end
end)

-- you can also disconnect this connection once its not needed anymore