Hello I tyried this using the Ray function and it didn't work.
game.Players.PlayerAdded:Connect(function(player) local invisiblePart = workspace.InvisiblePart local Ignore = player.Character while wait(2) do --You can change the wait value to anything local ray = Ray.new(player.Character.HumanoidRootPart.CFrame.p,Vector3.new(0,-5,0)) --Creating the ray down from the character local part = workspace:FindPartOnRay(Ray,Ignore) -- Finding the part that the ray hits if part then -- If there is a part if part == invisiblePart then -- If the part is the one you want to be invisible invisiblePart.Transpareny = 1 else invisiblePart.Transparency = 0 end end end)
First, you'll need an identifier to designate that a part shall turn invisble when looked at. Do this by right clicking on a part, and insert a BoolValue, rename it to "TurnInvisibleWhenLookedAt" without the quotes, and set it to true. Next, create a new LocalScript in StarterPack, and paste the code below into it.
RANGE = 100; lastPart = nil; lastTransparency = nil; Player = game:GetService("Players").LocalPlayer; while wait() do if (lastPart) then lastPart.Transparency = lastTransparency; end local camera = workspace.CurrentCamera; local ray = Ray.new(camera.CFrame.Position, camera.CFrame.LookVector * RANGE); local part, point = game.Workspace:FindPartOnRayWithIgnoreList(ray, Player.Character:GetChildren()); if part then local bool = part:FindFirstChild("TurnInvisibleWhenLookedAt"); if (bool and bool.Value) then lastTransparency = part.Transparency; part.Transparency = 1; lastPart = part; end end end
This script will shoot a ray from the player's camera at a given range, and if it hit a part, it will make sure the part is designated, and then it will store the part that it hit, along with the transparency of the part that it hit in order to revert them back later, and then change the transparency to 1 so that the part is invisible.