See if script contains certain text (script.Source)

If I wanted to see whether a script calls GetService() on a service, what would I do ?`

I tried this

 local sep = ' " ' -- separator

    local services = {[[ReplicatedStorage]],[[Workspace]], [[ReplicatedFirst]], [[Lighting]], [[ServerStorage]], [[ServerScriptService]],
    [[SoundService]], [[StarterGui]], [[Players]],[[ReplicatedScriptService]], [[SoundService]], [[StarterPack]], [[StarterPlayer]],
    [[Teams]], [[Chat]], [[LocalizationService]], [[TestService]]}  

    local s = workspace.LocalScript -- testing, just a placeholder; not actually in game

    local function ServiceIsInSource(ser)

    for _, v in ipairs(services) do 
             if ser:match(v) then
      if s.Source:find(":GetService("..sep..ser..sep..")") then
           return true end
       end
    end
         return false
    end

  print(ServiceIsInSource("ReplicatedStorage")) --> false

image

this is probably not a good approach (returns false everytime), could someone tell me how Iā€™d do this?
> code ran through the command bar

Is this for use in a game or in a plugin?

Script.Source is not readable by game scripts - only by plugins & command bar. See Script.Source here on the DevHub

If this is for a plugin, you should change your first line to
local sep = '"' -- separator to remove the spaces you have there right now - with your current code youā€™d be looking for :GetService( " ReplicatedStorage " )

Additionally, on the s.Source:find you should add the arguments 1, true as such:
if s.Source:find(":GetService("..sep..ser..sep..")", 1, true) then
:Find looks for a pattern, see here. The characters ( ) are reserved in these patterns as ā€˜magic charactersā€™. The 1 weā€™re adding in the argument just tells :Find what position to start the search and is necessary to allow us to put the true, which will ignore magic characters and treat the :match as a plain string. See here under string.find to see what Iā€™m talking about.

2 Likes

I think itā€™s because you included ..sep in the string, but you already included the seperator manually ("). So instead of searching for :GetService("ReplicatedStorage"), itā€™s searching for this: :GetService(""ReplicatedStorage"").

Try testing it without including the extra "s and only including the ..seps.

edit: oops i just realized iā€™m dumb

1 Like

1.
You should do some simple debugging, by printing out the search-string, so you can see what the find method is actually trying to search for.

local searchFor = ":GetService(" .. sep .. ser .. sep .. ")"
print(">" .. searchFor .. "<")
if s.Source:find(searchFor) then
  -- ...

2.
Another ā€œconfusingā€ thing, is that the string.find method defaults to using regular-expression for the search criteria. - Read/study the API docs for string.find. For regular-expressions, there is explanation of patterns on lua.org (Hint: the ( ) are special/magic characters in regular-expressions)

So to search using the verbatim content of the searchFor value, set the third argument of the string.find method to true:

if s.Source:find(searchFor, 1, true) then

(Hmm? Did J_B just edit his post, to include this information too?)

3 Likes
  print("GetService("..sep..service..sep..")") 

image

still doesnā€™t find, quotes appear twice only.

Using string.find that way didnā€™t work either

s.Source:find("game:GetService("..sep..service..sep..")",1,true) 

edit: works, previously used match