Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do you use variables in paths?

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.

1 comment

0

use :FindFIrstChild(tostring(pid))

1 answer

✓ Accepted answer

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)

1 comment

0

Thank you guys so much, this helped alot!

This discussion is preserved as it was. New answers, comments, and votes are closed.