GetPartsInPart nil error

I want to not have an error.
The issue is that there is an error: attempt to call a nil value On the 19th line of code.
I can’t find a solution.
code:

local runService = game:GetService("RunService")
local collectionService = game:GetService("CollectionService")

local waterGui = game:GetService("Players").LocalPlayer.PlayerGui:WaitForChild("ScreenGui").Water

local camera = workspace.CurrentCamera

local camPosPart = Instance.new("Part")
camPosPart.Parent = workspace
camPosPart.Name = "CameraPos"
camPosPart.Size = Vector3.new(0.001,0.001,0.001)

function touchingWater()
	camPosPart.CFrame = camera.CFrame

	local touching = workspace:GetPartsInPart(camPosPart)
	local notTouchingWater = 0

	if touching ~= nil then
		for i, part in pairs (touching) do
			if collectionService:HasTag(part, "Water") then
				waterGui.Visible = true
			else
				notTouchingWater += 1
			end
		end
		if notTouchingWater == #touching then
			waterGui.Visible = false
		end
	else
		waterGui.Visible = false
	end
end

runService.RenderStepped:Connect(touchingWater())

this is because you are connecting renderStepped to whatever the function returns, I assume you want to connect it to the function itself so

replace the last line
runService.RenderStepped:Connect(touchingWater())

with:
runService.RenderStepped:Connect(touchingWater)

Thank you, the problem has now been fixed.

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