Also p is another cube, NOT THE PLAYER. I just gave it the name player cause thats me. Also everything works fine like the bar shows in the right place, it's just the FindPartOnRay that is being the issue.

repeat wait() until game.Workspace

local s = script.Parent
local p = game.Workspace.Player

local d = game:GetService("Debris")

print("loaded")

while wait() do
	local dir = (s.Position - p.Position).Unit
	local dist = (s.Position - p.Position).Magnitude
	
--	if dist < 0 then
--		dist = (p.Position - s.Position).Magnitude
--	end
	
	print(dir * dist)
	
	print("self pos: ", s.Position, "plr pos: ", p.Position, "dir: ", dir, "dis: ", dist)
		
	local ray = Ray.new(s.Position, dir * dist)
	local beam = Instance.new("Part", game.Workspace)
	beam.Size = Vector3.new(0.1, 0.1, dist)
	beam.CFrame = CFrame.new(s.Position, p.Position) * CFrame.new(0, 0, -dist/2)
	beam.Anchored = true
	beam.Material = Enum.Material.Neon
	beam.CanCollide = false
	
	--local hitobj, hitpos, normal, material
	local hit, pos = game.Workspace:FindPartOnRay(ray, s)
	
	if hit then
		beam.BrickColor = BrickColor.Red()
		print("hit obj")
	else
		beam.BrickColor = BrickColor.Green()
		print("no obj")
	end
	
	d:AddItem(beam, 0.1)
	wait(0.1)
end

Edit: Found out it sorta works but only when the block is around the top of the wall infront of it.

Edit 2: Nope, nevermind.

Main Edit: What I noticed is that when the block goes to an y value of around its own size or higher, which is 2 or so, it goes red. Even though there isnt anything blocking it.