WorldPosition casually is nil after a time

local player = game.Players.LocalPlayer
local character = player.Character

local hrp = character:WaitForChild("HumanoidRootPart")


local UserInputService = game:GetService("UserInputService");

local char = player.Character or player.CharacterAdded:Wait();
local humanoid = char:WaitForChild("Humanoid");


--Create Beams--
for i,v in pairs(workspace:GetChildren()) do
if v.Name == "Meat" then
local start = Instance.new("Attachment")
start.Name = "start"
start.Parent = hrp

Beam = Instance.new("Beam")
Beam.Name = "Beam"
Beam.Parent = workspace
Beam.Texture = "rbxassetid://13413762237"
Beam.Brightness = 10
Beam.TextureLength = 1
Beam.TextureMode = "Static"
Beam.TextureSpeed = 1
Beam.FaceCamera = true
Beam.Attachment0 = start
Beam.Attachment1 = v.Attachment
Beam.Color = ColorSequence.new(Color3.fromRGB(255, 0, 0),Color3.fromRGB(255,0,0))
Beam.Width0 = 1
Beam.Width1 = 1
Beam.Enabled = false
end
end

-- Functions--
function Sniff()
	for i,v in pairs(workspace:GetChildren()) do
		if v.Name == "Meat" then
			v.Material = Enum.Material.Neon
		end
	end
	for i,v in pairs(workspace:GetChildren()) do
		if v:IsA("Beam") then
			local magnitudeA = (v.Attachment0.WorldPosition - v.Attachment1.WorldPosition).Magnitude
			if magnitudeA < 500 then
				v.Enabled = true
			end
		end
	end
end

function StopSniff()
	for i,v in pairs(workspace:GetChildren()) do
		if v.Name == "Meat" then
			v.Material = Enum.Material.Plastic
		end
	end
	for i,v in pairs(workspace:GetChildren()) do
		if v.Name == "Beam" then
			v.Enabled = false
		end
	end
end



-- Enable Sniff --
UserInputService.InputBegan:Connect(function(input, gameProccessed, IsTyping)
	if input.KeyCode == Enum.KeyCode.B and not gameProccessed then
		Sniff()
	end
end)

--Disable Sniff--
UserInputService.InputEnded:Connect(function(input, gameProccesed, IsTyping)
	if input.KeyCode == Enum.KeyCode.B then
		StopSniff()
	end	
end)

Everything works, it’s just that after a certain time, idk maybe 5 minutes, the world position is nil and obviously the functions do not work then. This is a scent system that makes a beam between the meat part and humanoidrootpart visible when B is being held.

I really have no idea what’s causing it, if anyone does find an error in my script, I’d be so thankful.

And yeah, using WorldPosition to limit the beams. So an animal cannot smell meat around the whole map, only if it’s less than 500 studs away

You are getting an error because v.Attachment0 or v.Attachment1 is nil, which means the Beam it’s going through doesn’t have both attachments selected. Because it happens after some time, I assume a stray Beam instance appears in workspace for some reason, without either one of the Attachments selected.