This should work for your intended purpose.
local MainPart = workspace.MainPart
local Camera = game.Workspace.CurrentCamera
local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Params = RaycastParams.new()
Params.FilterType = Enum.RaycastFilterType.Blacklist
Params.FilterDescendantsInstances = { Character }
local function Visibilty()
-- Check if the jumpscare object is on the player screen
local Vector, OnScreen = Camera:WorldToScreenPoint(MainPart.Position)
if not OnScreen then
return false
end
-- Cast a ray to the jumpscare object
local Result = workspace:Raycast(Camera.CFrame.Position, Camera.CFrame.LookVector * 500, Params)
-- Check if the raycast hit anything and if what was hit is the jumpscare object
if Result and Result.Instance == MainPart then
return true
end
end
while true do
local Visible = Visibilty()
if Visible then
task.wait(1)
MainPart:Destroy()
end
task.wait(1)
end