Check if one of the folder's part is touching something

Hi, I have this loop, i wanted to check if one of the folder’s part is touching something but it doesn’t print nothing

while wait() do
	for _,room in pairs(workspace:GetChildren()) do
		if room.Name == "CRoom" then
			for _,checkBlock in pairs(room:WaitForChild("ChecksBlocks"):GetChildren()) do
				for i,v in pairs(checkBlock:GetTouchingParts()) do
					print(v)
					for _,checkValue in pairs(room:WaitForChild("ChecksValues"):GetChildren()) do
						if checkValue.Name == checkBlock then
							checkValue.Value = true
						end
					end
				end
			end
		end
	end
end

Are there any errors? Or can you provide more info if it’s possible?

image

These 2 folders are contained in a Model, i want to check if one of the parts in CheckBlocks folder is touched. When touched, change the value to true to the value in the CheckValues folder with the same name.

it may be that I am taking the wrong approach

But what touches them? Your character? Did you set your blocks’ property “CanTouch” to true?

the Blocks are being touched by normal parts, not by your character.
CanTouch is set to true

Maybe it’s because you are comparing the checkValue’s name with checkBlock which is an object. Try typing this and see if it works:

checkValue.Name == checkBlock.Name

Apparently your folder names are “CheckValues” and :“CheckBlocks”

Here you got “ChecksBlocks”

1 Like

As well as “ChecksValues” instead of CheckValues

is definitely a step forward but unfortunately the script does not print out anything at the level of print(v)

1 Like

i made this with another method but the result is the same

while wait() do
	for i,v in pairs(workspace:GetChildren()) do
		if v.Name == "CRoom" then
			local CheckBlocks = v:WaitForChild("CheckBlocks")
			local CheckValues = v:WaitForChild("CheckValues")
			for i,v in pairs(CheckBlocks:GetChildren()) do
				v.Touched:Connect(function()
					print("touched")
					CheckValues:FindFirstChild(v.Name).Value = true
				end)
			end
		end
	end
end

it doesn’t print “touched”

1 Like

Have you set CanCollide to true or false?

The blocks have CanCollide set to false

That’s the problem. I tried your script and it doesn’t work only if you set CanCollide to false.
To get it to work I used SpatialQuerry.
Try to change this

To this:

for i,v in pairs(workspace:GetPartsInPart(checkBlock)) do
1 Like

Are your parts anchored? They may be falling through the world before you can touch it.

I tried it with this setup:

while wait() do
	for i,v in pairs(workspace:GetChildren()) do
		if v.Name == "CRoom" then
			local CheckBlocks = v:WaitForChild("CheckBlocks")
			local CheckValues = v:WaitForChild("CheckValues")
			for i,v in pairs(CheckBlocks:GetChildren()) do
				v.Touched:Connect(function()
					print("touched")
					CheckValues:FindFirstChild(v.Name).Value = true
				end)
			end
		end
	end
end

image

And it works fine without CanCollide.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.