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
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
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.
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.
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?)