Position values returning as (0,0,0)

Here is a localscript I made that’s located inside of the player’s character. What this is supposed to do is track the location of the player and the location of an npc, and compare the two positions to see if the distance between the two is less than 50. However, every time I go to print the two positions, they’re always labeled as (0,0,0) despite this definitely not being the case.

This might just be me being dumb, but is this because it is a localscript? Is there any way to solve this without the use of RemoteEvents? I would like to avoid firing a RemoteEvent every tick lol

My code:

--Localscript in player's character
local model = workspace.NPCModel
local mainpart = model.MainPart
local sound = workspace.sfxMonster.tw_scream
local character = script.Parent
local human = character:WaitForChild("Humanoid")

local camera = workspace.CurrentCamera
local lookdistance = 50

local function isPointVisible(worldPoint,characterPoint)
	local vector, onScreen = camera:WorldToViewportPoint(worldPoint)

	if onScreen then
		local origin = camera.CFrame.Position
		local ray = workspace:Raycast(origin, worldPoint - origin)
		local hit = ray and ray.Instance
		if hit then
			if (characterPoint - worldPoint).magnitude < lookdistance then
				if sound.Playing == false then
					sound:Play()
				end
			end
		end
	end
end

while human ~= nil do
	wait()
	local worldpoint = Vector3.new(mainpart.Position)
	local characterpoint = Vector3.new(character.HumanoidRootPart.Position)
	print(worldpoint) --Position returns (0,0,0)
	print(characterpoint) --Position returns (0,0,0)

	isPointVisible(worldpoint,characterpoint)
end

Any insight would be much appreciated.

You don’t need to call the constructor Vector3.new to create a new one. Using it is probably why you’re getting a Vector3 with values (0,0,0).

The Position property of the two parts is already a Vector3, so something like worldpoint = mainpart.Position and characterpoint = character.HumanoidRootPart.Position should work as intended.

2 Likes

Oh right, duh. I was following a guide for another thing which was using that in place of it which is probably why I messed it up. I’ll try it out later to make sure it works. Many thanks!!

Yeah, you’re attempting to pass Vector3 values to the Vector3 class constructor function even though its expected arguments are 3 number values, hence as a fail-safe the function is returning an “empty” Vector3 value which points towards the X, Y and Z position (0, 0, 0) in the worldspace.