How can I detect if there is a wait() function in a script?

I’m making an optimizer plugin, and I need to check if any scripts contain wait() and recommend to change them to task.wait(). How could I do this? I tried using string.find(Script.Source, 'wait%('), but that gives a false positive when scripts use task.wait(), and WaitForChild if it’s lowercase.

I also tried using the ^ magic character at the beginning of the find pattern, but that only works if the script’s first line is wait().

Any ideas? Is there another magic character that could help out here?

You could probably do something like this:

local beg = string.find(Script.Source, 'wait%(')
local beg2 = string.find(Script.Source, 'task%.wait%(')
if  beg ~= beg2+5 then
    --recommend
end
1 Like

Why is 5 being added?

task. is five letters long, meaning that the beginning of the wait( in task.wait() would be five letters ahead.

It causes an error:

Ah, you should probably add a check.

local beg = string.find(Script.Source, 'wait%(')
local beg2 = string.find(Script.Source, 'task%.wait%(')
if beg and (not beg2 or beg2 and beg ~= beg2+5) then
    --recommend
end

Sorry for the messy script. I edited a few things that I added and weren’t needed.
Edit: Okay, maybe it was needed. Edited the script again :sweat_smile:

Works, thank you!

An important note to other people doing this, though: If you are looping through game, it will also detect CoreGui, so you will have to individually loop through every common service. I did this by using CombinedPairs:

for _, Script in CombinedPairs(dsc(game:GetService('ReplicatedStorage')), dsc(game:GetService('Workspace')), dsc(game:GetService('Lighting')), dsc(game:GetService('ServerScriptService')), dsc(game:GetService('ServerStorage')), dsc(game:GetService('StarterPack')), dsc(game:GetService('StarterPlayer')), dsc(game:GetService('StarterGui')), dsc(game:GetService('ReplicatedFirst'))) do

It’s obviously… a little messy… but it does the trick.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.