JIT
Optimizing Roblox XRay script for performance
-- XRay Script for Roblox (Educational Purpose) -- Place this LocalScript in StarterPlayerScripts. -- This script makes buildings/parts semi-transparent to see through walls -- Press N to toggle XRay mode local Players = game:GetService("Players") local RunService = game:GetService("RunService") local UserInputService = game:GetService("UserInputService") local Workspace = game:GetService("Workspace") local LocalPlayer = Players.LocalPlayer -- Settings local XRayKey = Enum.KeyCode.N local TransparencyAmount = 0.5 -- 0 = fully visible, 1 = fully transparent local RefreshRate = 0.1 -- seconds between updates local MaxDistance = 2000 -- studs from player to affect parts local ExcludedNames = {"Terrain", "BasePlate"} -- Parts to exclude from XRay -- State local xRayEnabled = false local originalTransparency = {} local lastUpdate = 0 -- Helper: check if part should be excluded local function shouldExcludePart(part) for _, name in ipairs(ExcludedNames) do if part.Name == name then return true end end return false end -- Helper: check if part is visible (not completely transparent) local function isPartVisible(part) return part.Transparency < 0.95 -- Allow slight tolerance for nearly invisible parts end -- Helper: get all visible parts within range of player local function getVisiblePartsInRange() local parts = {} local character = LocalPlayer.Character if not character then return parts end local humanoidRootPart = character:FindFirstChild("HumanoidRootPart") if not humanoidRootPart then return parts end local playerPosition = humanoidRootPart.Position for _, part in ipairs(Workspace:GetDescendants()) do if part:IsA("BasePart") and not shouldExcludePart(part) and isPartVisible(part) then local distance = (part.Position - playerPosition).Magnitude if distance <= MaxDistance then table.insert(parts, part) end end end return parts end -- Enable XRay: make visible parts semi-transparent local function enableXRay() local parts = getVisiblePartsInRange() for _, part in ipairs(parts) do if not originalTransparency[part] then originalTransparency[part] = part.Transparency end part.Transparency = TransparencyAmount end xRayEnabled = true print("XRay Enabled - Visible parts made semi-transparent") end -- Disable XRay: restore original transparency local function disableXRay() for part, transparency in pairs(originalTransparency) do if part.Parent then -- Make sure part still exists part.Transparency = transparency end end originalTransparency = {} xRayEnabled = false print("XRay Disabled - Transparency restored") end -- Toggle XRay with N key UserInputService.InputBegan:Connect(function(input, gameProcessedEvent) if gameProcessedEvent then return end if input.UserInputType == Enum.UserInputType.Keyboard and input.KeyCode == XRayKey then if xRayEnabled then disableXRay() else enableXRay() end end end) -- Periodic update to maintain XRay effect and handle moving parts RunService.Heartbeat:Connect(function(step) if not xRayEnabled then return end lastUpdate = lastUpdate + step if lastUpdate >= RefreshRate then lastUpdate = 0 -- Update transparency for existing visible parts local parts = getVisiblePartsInRange() for _, part in ipairs(parts) do if not originalTransparency[part] then originalTransparency[part] = part.Transparency end part.Transparency = TransparencyAmount end -- Clean up references to destroyed parts for part in pairs(originalTransparency) do if not part.Parent then originalTransparency[part] = nil end end end end) -- Clean up when script is destroyed script.Destroying:Connect(function() if xRayEnabled then disableXRay() end end) make this script not lag or spike fps up and down, or freeze the screen.
Read file
Write file
Optimized XRay script performance
v1