How do I look for a variable in a string?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to find variables in a string and replace them
    Example:

“local baseplate = workspace.Baseplate”

turns into

“local baseplate = game.Lighting.Baseplate”

I only want it to find objects and also not just variables. I want it to also find things obscure like

print(workspace,workspace.Baseplate) ← finds the “workspace.Baseplate” here and replaces it with cheese.Baseplate or something

  1. What is the issue? Include screenshots / videos if possible!

I tried some methods but i cant get it perfect.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I used recursive stuff and gsub, split and whatever but i had no luck
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
function sandbox:getInstanceVariables(str)
	local spl = str:split(".")
	local insts = {}
	local start = 0
	for i,v in pairs(spl)do
		local f = false
		if v == "workspace"then
			v = "Workspace"
		end
		for s,ser in pairs(modules.services)do
			if s == v:match(s)then
				f = true
				v = s
				print(v)
			end
		end
		if f and spl[i+1]then
			local str = v.."."..spl[i+1]
			local ind = i+1
			local found
			while (not found and spl[ind]) do 
				print(str)
				for i,v in pairs(_G.inst)do
					if v:GetFullName() == str then
						found = v
						local new = ("_G.inst['%s']"):format(spl[ind])
						str = str:sub(1,start)..new..str:sub(start,#new+1)
					end
				end
				if not found then
					ind+=1
					str=str.."."..spl[i+1]
				end
			end
			if found then 
				table.insert(insts,found)
			end
		else 
			start+=#v
		end
	end
	return insts
end

–The code above works but doesn’t filter properly.

It gets the service and prints this

“ReplicatedStorage.Name = blah blah”

but it should be

“ReplicatedStorage”

_G.inst is a table of objects

modules.services is a module filled with sandboxed services - recreating game.

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

local newString = string.gsub(“local baseplate = workspace.Baseplate”, “workspace.Baseplate”, “game.Lighting.Baseplate”)
Now if we print newString it will print game.Lighting.Baseplate
(Reminder: Dont copy, its just how you could do it)

2 Likes

Thats not what im looking for.

I want it to find every instance that could be in a string.

Example:

“workspace.Baseplate” - flagged return Baseplate
“game.ReplicatedStorage.Event” - flagged return Event
“workspace.Part.Part.Part.Weld.Script.Highlight” - flagged. return Highlight

hm. I actually have a solution for this. let me code.

1 Like
local instance = "game.Workspace.Baseplate"
instance = string.gsub(instance, "game.", "")
instance = string.split(instance, ".")

local lastInstance = game
for i, v in pairs(instance) do
	lastInstance = lastInstance[v]
end
print(lastInstance) -- this will print Baseplate in this example.

(you can also say Workspace.Baseplate)

1 Like

Did this help you? @ScandalousSolution

1 Like

You’ll need to get a list of services which are referenced “game.ServiceName” and then use gsub to replace them from strings with nothing to get the instance path after the service reference itself.

1 Like
local path = "game.Workspace.Model.Part.Script"
local instance = string.match(path, ".([^%.]+)$")
print(instance)
--"Script" is output
1 Like

Only issue with thisnis that it would error in the script if the child does not exist. That may be an unwanted operation, so a slight modification would allow this to work pretty well.

local instance = "game.Workspace.Baseplate"
instance = instance:gsub("game.", "");
instance = instance:split(".")

local lastInstance = game
for i, v in pairs(instance) do
        if(lastinstance:FindFirstChild(v)) then
	    lastInstance = lastInstance[v];
        else
            print(v, "is not a member of", lastInstance:GetFullName());
            return;
        end
end
print(lastInstance) -- this will print Baseplate in this example.

Everyone here is getting it wrong. I am making a sandbox to run code but limit it so the user cant abuse. I need a function that finds every single instance inside a string, and returns the object
what i mean is

if a user runs:

print(workspace)
the script finds workspace as an instance and adds it to a table to be returned.

or maybe they put

game.RepliactedStorage.Remote:Destroy()
it will find the remote in replicated storage and returns it

and for big scripts like

print(workspace)
game.ReplicatedStorage.Name = "noob"
local s = game:GetService("Workspace")
print(s)

game:GetService(“Workspace”) is detected, return workspace
print(s) is not detected, because i only want it when its actually called to get an object
game.ReplicatedStorage.Name is detected, return replicatedstorage
workspace is detected, return workspace.

Thats an example. I don’t want it to detect services, just objects that might be inside of them.
Thank for your responses, although I don’t think it would be optimal if i checked through every single Instance in the game.

I know some crazy regex string pro has an answer to it, but its unlikely.