Calling workspace objects with variables

To keep it brief, I’m writing a script to spawn a part randomly in one of 4 tiles in one of 15 sets of 4 tiles, and I noticed that I cannot use variables when calling an object in the workspace, because instead of using the variable value in the object call, the script attempts to find something with the same name as the variable resulting in an error. Is there any way I can circumvent this short of restructuring my script so as to not use variables in object calls (which would probably take a whole lot more effort to write) I’d appreciate any help, and thank you all in advance.

while true do
	wait(10)
	local SpawnLocation1 = math.random(1,3)
	local SpawnLocation2 = math.random(1,5)
	print("the spawn tileset is "..SpawnLocation1.."/"..SpawnLocation2)
	local EncounterOrb = Instance.new("Part")
	local SpawnLocationName = "Tiles"..SpawnLocation1.."/"..SpawnLocation2
	local SpawnTileName = "Tile"..math.random(1,4)
	print(SpawnLocationName)
	EncounterOrb.Position = Vector3.new(game.Workspace.Folder.SpawnLocationName.SpawnTileName.x,game.Workspace.Folder.SpawnLocationName.SpawnTileName.y,game.Workspace.Folder.SpawnLocationName.SpawnTileName.z)
end

Do you want something more like this?

while true do
	wait(10)
	local SpawnLocation1 = math.random(1,3)
	local SpawnLocation2 = math.random(1,5)
	print("the spawn tileset is "..SpawnLocation1.."/"..SpawnLocation2)
	local EncounterOrb = Instance.new("Part")
	local SpawnLocationName = "Tiles"..SpawnLocation1.."/"..SpawnLocation2
    local TilesObj = game.Workspace.Folder:FindFirstChild(SpawnLocationName)
	local SpawnTileName = "Tile"..math.random(1,4)
    local TileObj = TilesObj:FindFirstChild(SpawnTileName)
	print(SpawnLocationName)
	EncounterOrb.Position = Vector3.new(TileObj.x,TileObj.y,TileObj.z)
end
1 Like

Oh man thanks a bunch, It’s been a while since I’ve worked in studio so I’ve forgotten a lot of things that should have been common sense. Works perfectly, and again, thanks.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.