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.