Hello fellow developers! I recently encountered a problem with my script, i have a module script that detects if the player is looking at a part or not, the problem i have here is that the script prints out that it dosent see the part when in first person. As the game is first person only i find this a big issue.
I searched everywhere and found no answer.
Module Script:
local module = {}
function module.ItemInView(plr, camera, checksubject, type_)
local partToCheck = checksubject
if game.Workspace:FindFirstChild(plr.Name) then
local char=game.Workspace:FindFirstChild(plr.Name)
if char:FindFirstChild("HumanoidRootPart") and char:FindFirstChild("Humanoid") then
-- Check if on screen:
local rootPart = char:FindFirstChild("HumanoidRootPart")
local humanid = char:FindFirstChild("Humanoid")
local vector, inViewport = camera:WorldToViewportPoint(partToCheck.Position)
local onScreen = inViewport and vector.Z > 0
-- Raycast outward:
local ray = camera:ViewportPointToRay(vector.X, vector.Y, 0)
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
raycastParams.FilterDescendantsInstances = {partToCheck, char}
--Big Difference
--stretch is the distance between the player & the part to check
local stretch = (partToCheck.Position - rootPart.Position).Magnitude
local raycastResult = workspace:Raycast(ray.Origin, ray.Direction * stretch, raycastParams)
-- Final check:
local isVisible
if type_ == "Screen" then
isVisible = onScreen
elseif type_ == "PlainSight" then
isVisible = onScreen and not raycastResult
end
--Big Difference
if isVisible then
-- Do something
local mag_distance = (partToCheck.Position - rootPart.Position).Magnitude
print("In Vision is true")
return {"true", mag_distance}
else
local mag_distance = (partToCheck.Position - rootPart.Position).Magnitude
print("In Vision is false")
return {"false", mag_distance}
end
--end the operation here
else
return {"false"}
end
else
return {"false"}
end
end
return module
Also the local script:
local module = require(game.ReplicatedStorage.Visible)
while wait(.1) do
local inview = module.ItemInView(game.Players.LocalPlayer, workspace.CurrentCamera, workspace.Assets.Open.Viewbox, "PlainSight")
if inview[1] == "true" then
print("Can see")
elseif inview[1] == "false" then
print("Can't see")
end
end
Thanks for anyone who can help me solve this!