"BoolValue is not a valid member of Part" when trying to check if that BoolValue doesn't exist

Hello,

I am having a few issues regarding checks for if a child exists or not. The code below is a snippet of a bomb script, which checks the surrounding parts in a radius if they should be unlocked and/or unanchored. This is used in multiple scripts that create explosions (rockets, landmines, etc.)

	-- create part and empty touch function
	local Part = Instance.new("Part")
	Part.Size = Vector3.new(12,12,12)
	Part.CFrame= script.Parent.CFrame -- same position as the explosion
	Part.Shape = Enum.PartType.Ball
	Part.Anchored = true
	Part.Transparency = 1
	Part.CanCollide = false
	Part.Parent = workspace

	Part.Touched:Connect(function()
	end)

	-- find every touching brick and process them
	local WithinExplosionRadius = Part:GetTouchingParts()
	Part:Destroy()
	for _,part in pairs(WithinExplosionRadius) do
		if part:FindFirstChild("NoUnanchor") == false then -- wait for child didn't work either [Infinite yield possible on 'Workspace.STUFF.blocks.grass:WaitForChild("NoUnanchor")']
			local NoUnanchorBool = Instance.new("BoolValue")
			NoUnanchorBool.Parent = part
			NoUnanchorBool.Value = false
		end
		if part:FindFirstChild("NoUnlock") == nil then -- tried this too, didn't work. 'if not' failed as well.
			local NoUnlockBool = Instance.new("BoolValue")
			NoUnlockBool.Parent = part
			NoUnlockBool.Value = false
		end
		if part.NoUnanchor.Value == false then
			part.Anchored = false
		end
		if part.NoUnlock.Value == false then
			part.Locked = false
		end
	end

I wanted to utilise BoolValues to check if the brick is allowed to be edited. I did not want to add those values to every brick that will spawn or be in the game (including some bricks that will be generated via other scripts). The parts that don’t have the BoolValues before the check are allowed to be changed, as they are most likely to not break the game.

Instead of this working, this code results in the following:
NoUnanchor is not a valid member of Part "Workspace.STUFF.blocks.grass"

It errors out and halts entirely. It’s very plausible that I missed something very important that’s needed.

I tried many things to make this work (making it a variable (local bool = part:FindFirstChild("NoUnlock")), wait for child instead of find first child, if not, == nil, == false) and I have run out of possible things to try.

Can you please help me figure out what went wrong? Is this a lost cause? Can BoolValues even be checked if they exist?

Regards, Dango360.

Remove the == false, and put a second if containing that inside of the FindFirstChild.

1 Like

Hey!

Essentially it didn’t work because you had misplaced the if statements. I’ve spruced it up for you.

Script
local Part = Instance.new("Part")
Part.Size = Vector3.new(12,12,12)
Part.CFrame= script.Parent.CFrame
Part.Shape = Enum.PartType.Ball
Part.Anchored = true
Part.Transparency = 1
Part.CanCollide = false
Part.Parent = workspace

Part.Touched:Connect(function()
end)

local WithinExplosionRadius = Part:GetTouchingParts()
Part:Destroy()

for _,part in pairs(WithinExplosionRadius) do
	if part:FindFirstChild("NoUnanchor") then
		if not part.NoUnanchor.Value then
			part.Anchored = false
		end
	else
		part.Anchored = false
	end
	if part:FindFirstChild("NoUnlock") then
		if not part.NoUnlock.Value then
			part.Locked = false
		end
	else
		part.Locked = false
	end
end
2 Likes

This is wasting bandwidth like Candy. The best solution to this is maybe making a whitelist of Directories of parts that can be destroyed. This post talks about this in depth.

1 Like