Raycasting (glitch?)

hi guys, i have this line of code and what it does: it check if there are parts next to the one thats getting deleted. but if the raycast returns nil somehow this if statement frontPart ~= nil still retuns a true. How?, please help me.

   script.Parent.Delete.OnServerEvent:Connect(function(plr, part, stage, color)
    	if stage == "Base" then
		print("Rays Begin!")
	
	local frontPart = CastRay(part.Position, part.CFrame.LookVector)
	local backPart = CastRay(part.Position, -part.CFrame.LookVector)
	local rightPart = CastRay(part.Position, part.CFrame.RightVector)
	local leftPart = CastRay(part.Position, -part.CFrame.RightVector)
	
	print("Rays Done!")

	if frontPart ~= nil then
		frontPart.Parent.BL.Transparency = 0
	end
	if backPart ~= nil then
		print(backPart )
		frontPart.Parent.FL.Transparency = 0
	end
	if rightPart ~= nil then
		print(rightPart )
		frontPart.Parent.LL.Transparency = 0
	end
	if leftPart ~= nil then
		print(leftPart )
		frontPart.Parent.RL.Transparency = 0
	end
	
	part.Parent:Destroy()
end
end)

Raycast method:

local function CastRay(from, to)
	local raycastParams = RaycastParams.new()
	raycastParams.FilterType = Enum.RaycastFilterType.Whitelist
	raycastParams.FilterDescendantsInstances = {workspace.Buildings}
	raycastParams.IgnoreWater = true

	-- Cast the ray
	local raycastResult = workspace:Raycast(from, to*12, raycastParams)

	-- Interpret the result
	if raycastResult then
		return raycastResult.Instance
	else
		return nil
	end
end

I forgot to tell. This script does work but it exactly the same but inverse:

script.Parent.Place.OnServerEvent:Connect(function(plr, pos, stage, color)
	if stage == "Base" then
		local newModel = repli.Base:Clone()
		newModel.PrimaryPart = newModel.Prime
		newModel.Parent = workspace.Buildings
		newModel:SetPrimaryPartCFrame(pos) 
		
		local part = newModel.PrimaryPart
		
		spawn(function()
			newModel.PrimaryPart.par.Enabled = true
			wait(.1)
			newModel.PrimaryPart.par.Enabled = false
		end)
		
		local frontPart = CastRay(part.Position, part.CFrame.LookVector)
		local backPart = CastRay(part.Position, -part.CFrame.LookVector)
		local rightPart = CastRay(part.Position, part.CFrame.RightVector)
		local leftPart = CastRay(part.Position, -part.CFrame.RightVector)
		
		if frontPart ~= nil then
			frontPart.Parent.BL.Transparency = 1
			part.Parent.FL.Transparency = 1
			frontPart.Parent.LineB.Transparency = 0
			part.Parent.LineF.Transparency = 0
		end
		if backPart ~= nil then
			backPart.Parent.FL.Transparency = 1
			part.Parent.BL.Transparency = 1
			backPart.Parent.LineF.Transparency = 0
			part.Parent.LineB.Transparency = 0
		end
		if rightPart ~= nil then
			rightPart.Parent.LL.Transparency = 1
			part.Parent.RL.Transparency = 1
			rightPart.Parent.LineL.Transparency = 0
			part.Parent.LineR.Transparency = 0
		end
		if leftPart ~= nil then
			leftPart.Parent.RL.Transparency = 1
			part.Parent.LL.Transparency = 1
			leftPart.Parent.LineR.Transparency = 0
			part.Parent.LineL.Transparency = 0
		end
	end
end)

dont use ~= nil simply put it like this

if frontPart then
		frontPart.Parent.BL.Transparency = 0
	end
	if backPart  then
		print(backPart )
		frontPart.Parent.FL.Transparency = 0
	end
	if rightPart  then
		print(rightPart )
		frontPart.Parent.LL.Transparency = 0
	end
	if leftPart  then
		print(leftPart )
		frontPart.Parent.RL.Transparency = 0
	end

oh yeah ur right nil is not needed. but still i implemented this and the problem remains.

nvm its fixed, i used the invilid return part.

1 Like