Use variable value to find model

So basically I’m new to coding, and I’m trying to make a system where a local script detects a model in workspace.camera and then selects a part in the model I want to edit. But the problem is the models name is based off tools in the players backpack, and the name of the model changes to the name of the tool equipped by the player at the time.

Screenshot 2022-01-24 003948
*edit sorry, “insert model name here” is suppose to be “ArmaClient”

“ArmaClient” is the name of the tool currently equipped by the player, I then tried to use the variable “Toolname” as it contains the name of the model to try and find the model in “workspace.camera” but instead of using the variables value as a name, it actually tries to find a object named “Toolname” in workspace.camera.

I looked everywhere but I cant seen to find a person with the same problem as me, this could also be a very simple error but I’m not that experienced with Lua to figure this out, please help me out.

You can index using brackets when using variables.

Toolname = "insert model name here"
local CM = workspace.Camera[Toolname][Toolname]

P.S. Use three ` characters to make code blocks, and past your code in them, it’s much easier to read for large chunks of code.

1 Like

You can use the :FindFirstChild method which accepts a string as the parameter:

local ToolName = "insert tool name here"
local objectInCamera = workspace.Camera:FindFirstChild(ToolName) 

print("Camera object:", objectInCamera)
1 Like

i tried your method and i received this error.
Screenshot 2022-01-24 005930
Screenshot 2022-01-24 005944
*edit sorry, i didnt see that i could make code blocks to make it easier to read

Toolname must be a string, therefore Tool.Name where Tool is the tool path.

1 Like