The script you provided runs an infinite loop with a wait(0,3) inside, which is incorrect syntax (presumably you meant wait(0.3)), and it iterates through all descendants of the Workspace every 0.3 seconds, modifying every ProximityPrompt found.
This can cause a lot of lag because:
- It loops through all descendants frequently.
- It runs an infinite loop on the main thread.
- It modifies value of all ProximityPrompts every iteration, even if they already have
HoldDuration=0.
How to reduce lag and improve efficiency:
- Fix the
wait function call.
- Cache or track ProximityPrompts to avoid iterating over all descendants repeatedly.
- Only set
HoldDuration if it's not already 0.
- Possibly connect to events when ProximityPrompts are added instead of continuously looping.
Here is a more optimized version:
local RunService = game:GetService("RunService")
local Workspace = game:GetService("Workspace")
-- Table to track processed ProximityPrompts so we don't repeat work
local processedPrompts = {}
-- Function to handle a new ProximityPrompt
local function processPrompt(prompt)
if prompt.HoldDuration ~= 0 then
prompt.HoldDuration = 0
end
processedPrompts[prompt] = true
end
-- On start, process existing prompts
for _, v in ipairs(Workspace:GetDescendants()) do
if v:IsA("ProximityPrompt") then
processPrompt(v)
end
end
-- Listen for new descendants added to Workspace
Workspace.DescendantAdded:Connect(function(descendant)
if descendant:IsA("ProximityPrompt") then
processPrompt(descendant)
end
end)
-- Optionally, listen for changed HoldDuration for existing prompts to keep forcing 0
-- But it depends on your use case. If you want to always keep HoldDuration=0, you can do this:
-- We'll periodically check only new prompts that might have changed (not mandatory, run less frequent than before)
RunService.Heartbeat:Connect(function()
for prompt, _ in pairs(processedPrompts) do
if prompt.HoldDuration ~= 0 then
prompt.HoldDuration = 0
end
end
end)
Explanation:
- We process and fix existing ProximityPrompts once on startup.
- We listen for new ProximityPrompts to process them only once when they are added.
- We keep a table to track processed prompts.
- The loop on
RunService.Heartbeat only iterates over this smaller set of prompts instead of all descendants, improving performance.
- The infinite
while true do loop is removed.
If you do not need to continuously enforce HoldDuration=0 (if it never changes after you set it), you can remove the RunService.Heartbeat section to reduce CPU use further.
If you want even less CPU use and the HoldDuration won't change after being set, just keep the first two parts (process on startup and on descendant added).