Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How do you use variables in paths?

Asked by 7 years ago

Iv'e been working on creating a system for my game where each player is assigned a unique plot ID And they can build on that plot. When a player joins they get a plot ID that corresponds with an Id Of one of the plots in the game. For the building, I have a local script that contains variables for retrieving the players plot ID and the selected block ID:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local pid = player.plotID.Value
local partid = player.buildID.Value

Now when I clone The part to the players plot I use the following code:

mouse.Button1Down:connect(function(click)
    if mouse.Target.Name == "plot" then
        if mouse.Target.id.Value == player.plotID.Value then
            local a = game.Lighting.Blocks.partid:Clone()
            a.Parent = game.Workspace.pid
            a.Position = Vector3.new(round(mouse.Hit.p.x), 1.5, round(mouse.Hit.p.z))
        end
    end
end)

I get the following error: pid is not a valid member of workspace, but I dont want it to look for pid in the workspace, I want it to look for game.workspace.(Player plot ID). How can I use the variable "pid" in the game path ? Thanks for your help.

0
use :FindFIrstChild(tostring(pid)) theCJarmy7 1293 — 7y

1 answer

Log in to vote
1
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
7 years ago

To substitute a variable in your path you can simply put brackets [ ] around the variable, if it's a string:

a.Parent = workspace[pid]

Note: This will return an error if 'pid' is nil


Alternatively, as theCJarmy7 stated, you can take advantage of the FindFirstChild function. Just use your variable as the parameters to the function:

a.Parent = workspace:FindFirstChild(pid)
0
Thank you guys so much, this helped alot! TheEnderShot 14 — 7y
Ad

Answer this question