JIT
Roblox XRay script optimization for 2000 studs
-- Optimized 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.5 -- Reduced from 0.1 to 0.5 seconds between updates local MaxDistance = 500 -- Reduced from 2000 to 500 studs from player local ExcludedNames = {"Terrain", "BasePlate"} -- Parts to exclude from XRay local ExcludedClassNames = {"Terrain", "Sky", "Camera"} -- Exclude by class too -- State local xRayEnabled = false local originalTransparency = {} local lastUpdate = 0 local trackedParts = {} -- Track parts we're managing -- Cache frequently used objects local excludedNameLookup = {} for _, name in ipairs(ExcludedNames) do excludedNameLookup[name] = true end local excludedClassLookup = {} for _, className in ipairs(ExcludedClassNames) do excludedClassLookup[className] = true end -- Helper: check if part should be excluded local function shouldExcludePart(part) return excludedNameLookup[part.Name] or excludedClassLookup[part.ClassName] end -- Helper: check if part is visible (not completely transparent) local function isPartVisible(part) return part.Transparency < 0.95 end -- Helper: more efficient part collection using spatial query instead of GetAllDescendants local function getVisiblePartsInRange() local character = LocalPlayer.Character if not character then return {} end local humanoidRootPart = character:FindFirstChild("HumanoidRootPart") if not humanoidRootPart then return {} end local playerPosition = humanoidRootPart.Position local parts = {} -- Use spatial query instead of iterating all descendants local region = Region3.new( playerPosition - Vector3.new(MaxDistance, MaxDistance, MaxDistance), playerPosition + Vector3.new(MaxDistance, MaxDistance, MaxDistance) ) local partsInRegion = Workspace:FindPartsInRegion3(region, nil, math.huge) for _, part in ipairs(partsInRegion) do if part:IsA("BasePart") and not shouldExcludePart(part) and isPartVisible(part) then local distance = (part.Position - playerPosition).Magnitude if distance <= MaxDistance then parts[part] = true end end end return parts end -- Enable XRay local function enableXRay() local visibleParts = getVisiblePartsInRange() for part in pairs(visibleParts) do if not originalTransparency[part] then originalTransparency[part] = part.Transparency end if not trackedParts[part] then part.Transparency = TransparencyAmount trackedParts[part] = true end end xRayEnabled = true end -- Disable XRay local function disableXRay() for part in pairs(trackedParts) do if part.Parent and originalTransparency[part] then part.Transparency = originalTransparency[part] end end originalTransparency = {} trackedParts = {} xRayEnabled = false 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) -- Optimized periodic update using task.delay instead of Heartbeat local function periodicUpdate() while true do if xRayEnabled then local visibleParts = getVisiblePartsInRange() -- Update transparency for current visible parts for part in pairs(visibleParts) do if not originalTransparency[part] then originalTransparency[part] = part.Transparency end part.Transparency = TransparencyAmount trackedParts[part] = true end -- Restore parts that are no longer in range for part in pairs(trackedParts) do if not visibleParts[part] and part.Parent and originalTransparency[part] then part.Transparency = originalTransparency[part] trackedParts[part] = nil end end -- Clean up destroyed parts for part in pairs(trackedParts) do if not part.Parent then trackedParts[part] = nil originalTransparency[part] = nil end end end task.wait(RefreshRate) end end -- Start the optimized update coroutine task.spawn(periodicUpdate) -- Clean up when script is destroyed script.Destroying:Connect(function() if xRayEnabled then disableXRay() end end) make this script have no lag no fps spikes but have it at the distance to atleast 2000 max (2000 studs)
Write file
Optimized XRay script for 2000 studs range with no lag
v1